feat(cli): 支持配置是否自动安装组件npm包,支持配置安装组件npm包后是否保持package.json不变

This commit is contained in:
roymondchen 2023-02-14 17:16:15 +08:00
parent 8369b2c22c
commit d06a874c3b
5 changed files with 59 additions and 34 deletions

View File

@ -10,4 +10,9 @@ cli({
temp: '.tmagic', temp: '.tmagic',
useTs: true, useTs: true,
dynamicImport: false, dynamicImport: false,
npmConfig: {
client: 'npm',
autoInstall: true,
keepPackageJsonClean: true,
},
}); });

View File

@ -22,12 +22,16 @@ export const scripts = (defaultAppConfig: UserConfig) => {
path.resolve(defaultAppConfig.temp, 'config.cjs'), path.resolve(defaultAppConfig.temp, 'config.cjs'),
].find((item) => fs.pathExistsSync(item)); ].find((item) => fs.pathExistsSync(item));
const userConfig = await loadUserConfig(userConfigPath); const { npmConfig = {}, ...userConfig } = await loadUserConfig(userConfigPath);
// resolve the final app config to use // resolve the final app config to use
const appConfig = { const appConfig = {
...defaultAppConfig, ...defaultAppConfig,
...userConfig, ...userConfig,
npmConfig: {
...(defaultAppConfig.npmConfig || {}),
...npmConfig,
},
}; };
if (appConfig === null) { if (appConfig === null) {

View File

@ -38,6 +38,10 @@ export interface NpmConfig {
registry?: string; registry?: string;
/** pnpm | npm | yarn */ /** pnpm | npm | yarn */
client?: 'npm' | 'yarn' | 'pnpm'; client?: 'npm' | 'yarn' | 'pnpm';
/** 是否自动安装组件依赖默认为true */
autoInstall?: boolean;
/** 安装组件后npm默认会将依赖写入package.json中将该值设置为true则不会写入默认为true */
keepPackageJsonClean?: boolean;
} }
export interface ModuleMainFilePath { export interface ModuleMainFilePath {

View File

@ -22,7 +22,13 @@ export const prepareEntryFile = async (app: App) => {
} }
Object.keys(contentMap).forEach((file: string) => { Object.keys(contentMap).forEach((file: string) => {
const fileName = `${file}.${useTs ? 'ts' : 'js'}`; let fileName = `${file}.ts`;
if (useTs) {
app.writeTemp(fileName, contentMap[file]);
} else {
fileName = `${file}.js`;
app.writeTemp(`${file}.d.ts`, `const type: Record<string, any>;\n\nexport default type;`);
}
app.writeTemp(fileName, contentMap[file]); app.writeTemp(fileName, contentMap[file]);
}); });
}; };

View File

@ -82,7 +82,9 @@ export const resolveAppPackages = (app: App): ModuleMainFilePath => {
dependencies[moduleName] = version; dependencies[moduleName] = version;
}; };
app.options.packages.forEach((item) => { const { packages = [], npmConfig = {} } = app.options;
packages.forEach((item) => {
if (typeof item === 'object') { if (typeof item === 'object') {
Object.entries(item).forEach(([, packagePath]) => { Object.entries(item).forEach(([, packagePath]) => {
getDependencies(packagePath); getDependencies(packagePath);
@ -92,26 +94,26 @@ export const resolveAppPackages = (app: App): ModuleMainFilePath => {
} }
}); });
if (Object.keys(dependencies).length) { if (npmConfig.autoInstall && Object.keys(dependencies).length) {
const packageFile = path.join(app.options.source, 'package.json'); if (!npmConfig.keepPackageJsonClean) {
const packageBakFile = path.join(app.options.source, 'package.json.bak');
if (fs.existsSync(packageFile)) {
fs.copyFileSync(packageFile, packageBakFile);
}
try {
npmInstall(dependencies, app.options.source, app.options.npmConfig); npmInstall(dependencies, app.options.source, app.options.npmConfig);
} catch (e) { } else {
error(e as string); const packageFile = path.join(app.options.source, 'package.json');
} const packageBakFile = path.join(app.options.source, 'package.json.bak');
if (fs.existsSync(packageFile)) {
fs.copyFileSync(packageFile, packageBakFile);
}
if (fs.existsSync(packageBakFile)) { npmInstall(dependencies, app.options.source, app.options.npmConfig);
fs.unlinkSync(packageFile);
fs.renameSync(packageBakFile, packageFile); if (fs.existsSync(packageBakFile)) {
fs.unlinkSync(packageFile);
fs.renameSync(packageBakFile, packageFile);
}
} }
} }
app.options.packages.forEach((item) => { packages.forEach((item) => {
if (typeof item === 'object') { if (typeof item === 'object') {
Object.entries(item).forEach(([key, packagePath]) => { Object.entries(item).forEach(([key, packagePath]) => {
setPackages(app.options.source, packagePath, key); setPackages(app.options.source, packagePath, key);
@ -131,26 +133,30 @@ export const resolveAppPackages = (app: App): ModuleMainFilePath => {
}; };
const npmInstall = function (dependencies: Record<string, string>, cwd: string, npmConfig: NpmConfig = {}) { const npmInstall = function (dependencies: Record<string, string>, cwd: string, npmConfig: NpmConfig = {}) {
const { client = 'npm', registry = 'https://registry.npmjs.org/' } = npmConfig; try {
const install = { const { client = 'npm', registry = 'https://registry.npmjs.org/' } = npmConfig;
npm: 'install', const install = {
yarn: 'add', npm: 'install',
pnpm: 'add', yarn: 'add',
}[client]; pnpm: 'add',
}[client];
const packages = Object.entries(dependencies) const packages = Object.entries(dependencies)
.map(([name, version]) => `${name}@${version}`) .map(([name, version]) => `${name}@${version}`)
.join(' '); .join(' ');
const command = `${client} ${install} ${packages} --registry ${registry}`; const command = `${client} ${install} ${packages} --registry ${registry}`;
execInfo(cwd); execInfo(cwd);
execInfo(command); execInfo(command);
execSync(command, { execSync(command, {
stdio: 'inherit', stdio: 'inherit',
cwd, cwd,
}); });
} catch (e) {
error(e as string);
}
}; };
/** /**