import * as path from 'path' ; import { lstatSync , pathExists , readdir } from 'fs-extra' ; import { IPluginAPI } from 'build-scripts' ; import { curry } from 'lodash' ; import * as postcssValueParser from 'postcss-value-parser' ; param { String } prefix param { String } name return { Boolean } const detectCssFile = curry ( ( prefix , name ) : boolean => { const filePath = path . resolve ( prefix , name ) ; const stat = lstatSync ( filePath ) ; return ! stat . isDirectory ( ) && name . split ( '.' ) . pop ( ) === 'css' ; } ) ; /**
 * 通过文件路径名称获取主题名
 */ export const getNameFromPath = ( filePath ) => { const arr = filePath . replace ( /^.*[\\/]/ , '' ) . split ( '.' ) ; arr . pop ( ) ; return arr . join ( '.' ) ; } ; param { String } names param { String } themeProperty export const getDefaultTheme = ( names , themeProperty ) => { const isExist = names . some ( name => name === themeProperty ) ; return { isExist , defaultName : isExist ? themeProperty : names [ 0 ] } ; } ; param { String } themesPath export const checkThemesEnabled = async ( themesPath ) : Promise < boolean > => { const dirExists = await pathExists ( themesPath ) ; if ( ! dirExists ) return false ; const files = await readdir ( themesPath ) ; if ( files . length === 0 ) return false ; const stylesExists = files . some ( detectCssFile ( themesPath ) ) ; if ( ! stylesExists ) return false ; return true ; } ; export const getFunction = ( str ) : { name : string ; params : string [ ] ; } => { const { nodes } = postcssValueParser ( str ) ; const node = nodes [ 0 ] ; const params = node ?. nodes ? node . nodes . filter ( ( i ) => i . type === 'word' || i . type === 'function' ) . reduce ( ( acc , item ) => { if ( item . type === 'word' ) { acc . push ( item . value ) ; } if ( item . type === 'function' ) { acc . push ( postcssValueParser . stringify ( item ) ) ; } return acc ; } , [ ] ) : [ ] ; return { name : node . value , params } ; } ; /**
 * 判断该字符串是否为函数调用时的形态
 *
 * eg: rgb(0,0,0,1) -> true
 */ export const isFunction = ( str ) => { const { nodes } = postcssValueParser ( str ) ; const node = nodes [ 0 ] ; return node ?. type === 'function' ; } ; export const getThemesName = async ( themesPath ) => { const files = await readdir ( themesPath ) ; const themesPathList = files . filter ( detectCssFile ( themesPath ) ) . map ( file => path . resolve ( themesPath , file ) ) ; return { themesPathList , themesNames : themesPathList . map ( getNameFromPath ) } ; } ; /**
 * 封装在插件目录下新建模板的逻辑
 */ export const addTemp = ( applyMethod , defaultName , themes ) => { const themesStr = themes . map ( str => `'${ str }'` ) . join ( ' | ' ) ; const templateSourceDir = path . join ( __dirname , '../../template' ) ; applyMethod ( 'addPluginTemplate' , templateSourceDir , { themes : themesStr , defaultTheme : `'${ defaultName }'` } ) ; } ;