async function processFile(filePath) { if (!fs.existsSync(filePath)) { return } const dirname = path.dirname(filePath) const distDirname = dirname.replace(sourceDir, tempDir) let distPath = path.format({dir: distDirname, base: path.basename(filePath)}) const code = fs.readFileSync(filePath, 'utf-8') if (REG_STYLE.test(filePath)) { // do something } else if (REG_SCRIPTS.test(filePath)) { if (REG_TYPESCRIPT.test(filePath)) { distPath = distPath.replace(/\.(tsx|ts)(\?.*)?$/, '.js') } Util.printLog(processTypeEnum.COMPILE, _.camelCase(path.extname(filePath)).toUpperCase(), filePath) // transformJSCode const transformResult = transformJSCode({code, filePath, isEntryFile: isEntryFile(filePath), projectConfig}) const jsCode = transformResult.code fs.ensureDirSync(distDirname) fs.writeFileSync(distPath, Buffer.from(jsCode)) // compileDepStyles const styleFiles = transformResult.styleFiles depTree[filePath] = styleFiles await compileDepStyles(filePath, styleFiles) } else { fs.ensureDirSync(distDirname) fs.copySync(filePath, distPath) Util.printLog(processTypeEnum.COPY, _.camelCase(path.extname(filePath)).toUpperCase(), filePath) } }
export function rename(p: string, options: {dir?: string, ext?: string}): string { let {dir, name, ext} = path.parse(p) if (options.dir != null) dir = options.dir if (options.ext != null) ext = options.ext return path.format({dir, name, ext}) }
/** * 返回通过新的扩展名与 request.dest的目标绝对路径生成新的 dest目标绝对路径 和 destRelative目标相对路径 * * @param {string} ext * @returns * @memberof WxSFM */ getDester (ext: string) { let ppath = path.parse(this.request.dest) ppath.base = ppath.name + ext ppath.ext = ext let dest = path.format(ppath) let destRelative = path.relative(config.cwd, dest) return { dest, destRelative } }
function copyFileToDist (filePath: string, sourceDir: string, outputDir: string) { if (!filePath && !path.isAbsolute(filePath)) { return } const { appPath } = buildData const dirname = path.dirname(filePath) const distDirname = dirname.replace(sourceDir, outputDir) const relativePath = path.relative(appPath, filePath) printLog(processTypeEnum.COPY, '发现文件', relativePath) fs.ensureDirSync(distDirname) fs.copyFileSync(filePath, path.format({ dir: distDirname, base: path.basename(filePath) })) }
async function processFile ({ filePath, tempPath, entryBaseName }) { const indexJsStr = ` import {AppRegistry} from 'react-native'; import App from '../${entryBaseName}'; // import {name as appName} from '../app.json'; AppRegistry.registerComponent('${moduleName}', () => App);` if (!fs.existsSync(filePath)) { return } const dirname = path.dirname(filePath) const destDirname = dirname.replace(tempPath, jdreactPath) const destFilePath = path.format({dir: destDirname, base: path.basename(filePath)}) const indexFilePath = path.join(tempPath, 'index.js') const tempPkgPath = path.join(tempPath, 'package.json') // generate jsbundles/moduleName.js if (filePath === indexFilePath) { const indexDistDirPath = path.join(jdreactPath, 'jsbundles') const indexDistFilePath = path.join(indexDistDirPath, `${moduleName}.js`) fs.ensureDirSync(indexDistDirPath) fs.writeFileSync(indexDistFilePath, indexJsStr) Util.printLog(processTypeEnum.GENERATE, `${moduleName}.js`, indexDistFilePath) return } // genetate package.json if (filePath === tempPkgPath) { const destPkgPath = path.join(jdreactPath, 'package.json') const templatePkgPath = path.join(jdreactTmpDirname, 'pkg') const tempPkgObject = fs.readJsonSync(tempPkgPath) const templatePkgObject = fs.readJsonSync(templatePkgPath) templatePkgObject.name = `jdreact-jsbundle-${moduleName}` templatePkgObject.dependencies = Object.assign({}, tempPkgObject.dependencies, templatePkgObject.dependencies) fs.writeJsonSync(destPkgPath, templatePkgObject, {spaces: 2}) Util.printLog(processTypeEnum.GENERATE, 'package.json', destPkgPath) return } fs.ensureDirSync(destDirname) fs.copySync(filePath, destFilePath) Util.printLog(processTypeEnum.COPY, _.camelCase(path.extname(filePath)).toUpperCase(), filePath) }
async function buildForWeapp () { const { appPath, entryFilePath, outputDirName, entryFileName, sourceDir } = buildData console.log() console.log(chalk.green('开始编译小程序端组件库!')) if (!fs.existsSync(entryFilePath)) { console.log(chalk.red('入口文件不存在,请检查!')) return } try { const outputDir = path.join(appPath, outputDirName, weappOutputName) const outputEntryFilePath = path.join(outputDir, entryFileName) const code = fs.readFileSync(entryFilePath).toString() const transformResult = wxTransformer({ code, sourcePath: entryFilePath, outputPath: outputEntryFilePath, isNormal: true, isTyped: REG_TYPESCRIPT.test(entryFilePath) }) const { styleFiles, components } = parseEntryAst(transformResult.ast, entryFilePath) if (styleFiles.length) { const outputStylePath = path.join(outputDir, 'css', 'index.css') await compileDepStyles(outputStylePath, styleFiles) } const relativePath = path.relative(appPath, entryFilePath) printLog(processTypeEnum.COPY, '发现文件', relativePath) fs.ensureDirSync(path.dirname(outputEntryFilePath)) fs.copyFileSync(entryFilePath, path.format({ dir: path.dirname(outputEntryFilePath), base: path.basename(outputEntryFilePath) })) if (components.length) { components.forEach(item => { copyFileToDist(item.path as string, sourceDir, outputDir) }) analyzeFiles(components.map(item => item.path as string), sourceDir, outputDir) } } catch (err) { console.log(err) } }
// } path.parse('C:\\path\\dir\\index.html') // returns // { // root : "C:\", // dir : "C:\path\dir", // base : "index.html", // ext : ".html", // name : "index" // } path.format({ root: "/", dir: "/home/user/dir", base: "file.txt", ext: ".txt", name: "file" }); // returns // '/home/user/dir/file.txt' } //////////////////////////////////////////////////// /// readline tests : https://nodejs.org/api/readline.html //////////////////////////////////////////////////// namespace readline_tests { let rl: readline.ReadLine; {