crootfast
Version:
大前端工程化命令行脚手架
235 lines (222 loc) • 9.98 kB
JavaScript
const shelljs = require('shelljs');
const { commandLineInterface, shelljsChildProcessSpawn } = require('../common/utils');
const { checkFileExists, checkNecFile, checkDirExists } = require('../common/utils/checkers');
const { writeToFolder, checkDirectoryExists } = require('../common/utils/file');
const { getSubdirectories } = require('../common/utils/infoFileSystem');
const { getGlobal, pathJoin, getclipath, getpropath, getBaseName } = require('../common/utils/path');
const { commandLoggerError, commandLoggerWarning, commandLoggerSuccess, isWin } = require('./cmd');
function createWebPackProject(name) {
let result = shelljs.cp('-r', `${getGlobal('crootdir')}/template`, `${getGlobal('prodir')}/${name}`);
commandLoggerWarning('正在执行模板下载命令...');
if (result.stderr) {
// console.error("Error copying files: ", result.stderr);
commandLoggerError('拷贝异常!', result.stderr);
} else {
commandLoggerSuccess('模板下载成功。');
}
// return commandLineInterface('cp', ['-r', `${getGlobal('crootdir')}/template`, `${getGlobal('prodir')}/${name}`], {
// start: '正在执行模板下载命令...',
// success: '模板下载成功。'
// }).then(resp => { });
};
// 安装所有环境依赖包,相当于 pnpm i
function installAllEnvironmentPackage(name) {
if (!checkNecFile('package.json', '.crootrc.js')) return;
// 找到用户项目根目录下的package文件
const userPackage = require(pathJoin('package.json'));
console.log('userPackage', userPackage);
if (!name) {
// let pack = checkFileExists('package.json');
// let workspace = checkFileExists('pnpm-workspace.yaml');
return commandLineInterface('pnpm', ['install', '-w'], {
start: '正在进行依赖包安装...',
success: '全部依赖包安装完毕。'
});
}
return commandLineInterface('bash', ['-c', `cd ${getGlobal('prodir')}/${name} && pnpm install -w`], {
start: '正在执行shell语句,对依赖包进行安装...',
success: '全部依赖包安装完毕。'
});
};
// 通过脚手架工具对工程中的项目进行依赖安装
async function toCommanderLineInterfaceInstallAllPackage() {
// console.log(webRoot, getpropath('.'), checkDirExists(webClient));
let webPack = pathJoin('.', getclipath('./webProject'));
let webpro = pathJoin('.', getpropath('.'));
// 映射的文件存在
if (checkDirExists(webpro)) { // && cd ${getpropath('.')
return isWin().then(async (stat) => {
if (stat) {
await shelljsChildProcessSpawn('cd', `/d ${webPack} && pnpm install`);
let dirlist = [
...getSubdirectories(pathJoin('apps/react', webpro)),
...getSubdirectories(pathJoin('apps/vue3', webpro))
];
for (let i = 0, len = dirlist.length; i < len; i++) {
let itPath = dirlist[i];
let appname = pathJoin(`apps/react/${getBaseName(itPath)}`, webpro);
// console.log(itPath, getBaseName(itPath), appname, getpropath('.'), getclipath('.'));
await shelljsChildProcessSpawn('cd', `/d ${appname} && pnpm install`);
}
// // 映射文件中所有的项目依赖安装
// let dirlistReact = [
// ...getSubdirectories(pathJoin('apps/react', webClient))
// ];
// // console.log('dirlist', dirlist);
// for (let i = 0, len = dirlistReact.length; i < len; i++) {
// let itPath = dirlistReact[i];
// let appname = pathJoin(`apps/react/${getBaseName(itPath)}`, getpropath('.'));
// // console.log(itPath, getBaseName(itPath), appname, getpropath('.'), getclipath('.'));
// await shelljsChildProcessSpawn('cd', `/d ${appname} && pnpm install`);
// }
// let dirlistVue3 = [
// ...getSubdirectories(pathJoin('apps/vue3', webClient))
// ];
// // console.log('dirlist', dirlist);
// for (let i = 0, len = dirlistVue3.length; i < len; i++) {
// let itPath = dirlistVue3[i];
// let appname = pathJoin(`apps/vue3/${getBaseName(itPath)}`, getpropath('.'));
// // console.log(itPath, getBaseName(itPath), appname, getpropath('.'), getclipath('.'));
// await shelljsChildProcessSpawn('cd', `/d ${appname} && pnpm install`);
// }
} else {
await commandLineInterface('bash', ['-c', `cd ${webPack} && pnpm install`], {
start: '正在通过脚手架对基座进行依赖安装',
success: ''
}, false);
// 映射文件中所有的项目依赖安装
let dirlist = [
...getSubdirectories(pathJoin('apps/react', webpro)),
...getSubdirectories(pathJoin('apps/vue3', webpro))
];
// console.log('dirlist', dirlist);
for (let i = 0, len = dirlist.length; i < len; i++) {
let itPath = dirlist[i];
// console.log(itPath, getpropath('.'));
await commandLineInterface('bash', ['-c', `cd ${itPath} && pnpm install && cd ${getclipath('.')}`], {
start: '正在通过脚手架对全部项目进行依赖安装...',
success: '全部依赖包安装完毕。'
});
}
}
})
} else {
commandLoggerError("目录出现异常,请检查。");
process.exit(1);
}
};
async function toCommanderLineInterfaceToProjectInstallPackage(frame, appname, package, flags) {
// let webPack = pathJoin('.', getclipath('./webProject'));
let webClient = pathJoin('.', getpropath('.'));
// console.log('webAppName', webAppName);
return isWin().then(async (stat) => {
if (stat) {
if (checkDirExists(webClient)) {
let webAppName = pathJoin(`apps/${frame}/${appname}`, getpropath('.'));
await shelljsChildProcessSpawn('cd', `/d ${webAppName} && pnpm install ${package} ${flags ?? '-S'}`);
} else {
commandLoggerError("软链接目录出现异常,请检查。");
process.exit(1);
}
} else {
if (checkDirExists(webClient)) {
let webAppName = pathJoin(`apps/${frame}/${appname}`, webClient);
await commandLineInterface('bash', ['-c', `cd ${webAppName} && pnpm install ${package} ${flags ?? '-S'} && cd ${getpropath('.')}`], {
start: `正在通过脚手架对项目${frame}/${appname}进行依赖安装。`,
success: `${frame}/${appname}的依赖包安装完成。`
});
} else {
commandLoggerError("目录出现异常,请检查");
process.exit(1);
}
}
})
};
// 新增一个项目到工程中
function toCommanderLineInterfaceAddProject(proname, frame) {
let apps = pathJoin('apps', pathJoin('.', getclipath('./template')));
let proapps = pathJoin('apps', pathJoin('.', getpropath('.')));
commandLoggerWarning('正在执行项目创建命令...');
console.log(`${apps}/${frame}/demo`);
console.log(`${proapps}/${frame}/${proname}`);
console.log(checkDirectoryExists(`${proapps}/${frame}/${proname}`));
if (!checkDirectoryExists(`${proapps}/${frame}/${proname}`)) {
shelljs.cp('-r', `${apps}/${frame}/demo`, `${proapps}/${frame}/${proname}`);
writeToFolder(`${proapps}/${frame}/${proname}/package.json`, JSON.stringify({
"name": proname,
"version": "1.0.0",
"description": `pnpm i --filter ${proname}`,
"main": "index.js",
"scripts": {},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {}
}, null, 2), true);
commandLoggerSuccess('项目创建成功。');
} else {
commandLoggerSuccess('项目已经存在。');
};
};
// 启动项目进行开发
async function toCommanderLineInterfaceDevelop(frame, appname, env) {
// if (checkDirExists(pathJoin('client', webPack))) {
// } else {
// commandLoggerError("软链接目录出现异常,请检查。");
// process.exit(1);
// }
let webPack = pathJoin('.', getclipath('./webProject'));
return isWin().then(async (stat) => {
if (stat) {
await shelljsChildProcessSpawn('cd', `/d ${webPack} && npm run start --frame=${frame} --appname=${appname} --env=${env}`);
} else {
await commandLineInterface('bash', ['-c', `cd ${webPack} && npm run start --frame=${frame} --appname=${appname} --env=${env}`], {
start: `正在通过脚手架启动${frame}/${appname}项目...`
}, !0);
}
})
}
// 打包构建项目
async function toCommanderLineInterfaceBuild(frame, appname, env) {
let webPack = pathJoin('.', getclipath('./webProject'));
// let webpro = pathJoin('.', getpropath('.'));
if (checkDirExists(pathJoin('.', webPack))) {
return isWin().then(async (stat) => {
if (stat) {
await shelljsChildProcessSpawn('cd', `/d ${webPack} && npm run build --frame=${frame} --appname=${appname} --env=${env}`); // && move ${webPack}/dist ${webpro}
} else {
// && mv ${webPack}/dist ${webpro}
await commandLineInterface('bash', ['-c', `cd ${webPack} && npm run build --frame=${frame} --appname=${appname} --env=${env}`], {
start: `正在通过脚手架启动${frame}/${appname}项目...`
}, !1);
}
})
} else {
commandLoggerError("目录出现异常,请检查。");
process.exit(1);
}
};
// cli 本身的依赖版本安装
async function crootCommanderLineInterface() {
let webCrootCLI = pathJoin('.', getclipath('.'));
if (checkDirExists(pathJoin('bin', webCrootCLI))) {
return await commandLineInterface('pnpm', [`install`], {
start: ``,
success: ``
});
} else {
commandLoggerError("croot cli install error.");
return Promise.reject(process.exit(1));
// process.exit(1);
}
};
module.exports = {
createWebPackProject,
installAllEnvironmentPackage,
toCommanderLineInterfaceInstallAllPackage,
toCommanderLineInterfaceToProjectInstallPackage,
toCommanderLineInterfaceDevelop,
toCommanderLineInterfaceBuild,
crootCommanderLineInterface,
toCommanderLineInterfaceAddProject
};