mirror of
https://github.com/Tencent/tmagic-editor.git
synced 2025-04-05 19:41:40 +08:00
feat(cli): 支持配置是否自动安装组件npm包,支持配置安装组件npm包后是否保持package.json不变
This commit is contained in:
parent
8369b2c22c
commit
d06a874c3b
@ -10,4 +10,9 @@ cli({
|
||||
temp: '.tmagic',
|
||||
useTs: true,
|
||||
dynamicImport: false,
|
||||
npmConfig: {
|
||||
client: 'npm',
|
||||
autoInstall: true,
|
||||
keepPackageJsonClean: true,
|
||||
},
|
||||
});
|
||||
|
@ -22,12 +22,16 @@ export const scripts = (defaultAppConfig: UserConfig) => {
|
||||
path.resolve(defaultAppConfig.temp, 'config.cjs'),
|
||||
].find((item) => fs.pathExistsSync(item));
|
||||
|
||||
const userConfig = await loadUserConfig(userConfigPath);
|
||||
const { npmConfig = {}, ...userConfig } = await loadUserConfig(userConfigPath);
|
||||
|
||||
// resolve the final app config to use
|
||||
const appConfig = {
|
||||
...defaultAppConfig,
|
||||
...userConfig,
|
||||
npmConfig: {
|
||||
...(defaultAppConfig.npmConfig || {}),
|
||||
...npmConfig,
|
||||
},
|
||||
};
|
||||
|
||||
if (appConfig === null) {
|
||||
|
@ -38,6 +38,10 @@ export interface NpmConfig {
|
||||
registry?: string;
|
||||
/** pnpm | npm | yarn */
|
||||
client?: 'npm' | 'yarn' | 'pnpm';
|
||||
/** 是否自动安装组件依赖,默认为true */
|
||||
autoInstall?: boolean;
|
||||
/** 安装组件后,npm默认会将依赖写入package.json中,将该值设置为true,则不会写入,默认为true */
|
||||
keepPackageJsonClean?: boolean;
|
||||
}
|
||||
|
||||
export interface ModuleMainFilePath {
|
||||
|
@ -22,7 +22,13 @@ export const prepareEntryFile = async (app: App) => {
|
||||
}
|
||||
|
||||
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]);
|
||||
});
|
||||
};
|
||||
|
@ -82,7 +82,9 @@ export const resolveAppPackages = (app: App): ModuleMainFilePath => {
|
||||
dependencies[moduleName] = version;
|
||||
};
|
||||
|
||||
app.options.packages.forEach((item) => {
|
||||
const { packages = [], npmConfig = {} } = app.options;
|
||||
|
||||
packages.forEach((item) => {
|
||||
if (typeof item === 'object') {
|
||||
Object.entries(item).forEach(([, packagePath]) => {
|
||||
getDependencies(packagePath);
|
||||
@ -92,26 +94,26 @@ export const resolveAppPackages = (app: App): ModuleMainFilePath => {
|
||||
}
|
||||
});
|
||||
|
||||
if (Object.keys(dependencies).length) {
|
||||
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);
|
||||
}
|
||||
|
||||
try {
|
||||
if (npmConfig.autoInstall && Object.keys(dependencies).length) {
|
||||
if (!npmConfig.keepPackageJsonClean) {
|
||||
npmInstall(dependencies, app.options.source, app.options.npmConfig);
|
||||
} catch (e) {
|
||||
error(e as string);
|
||||
}
|
||||
} else {
|
||||
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)) {
|
||||
fs.unlinkSync(packageFile);
|
||||
fs.renameSync(packageBakFile, packageFile);
|
||||
npmInstall(dependencies, app.options.source, app.options.npmConfig);
|
||||
|
||||
if (fs.existsSync(packageBakFile)) {
|
||||
fs.unlinkSync(packageFile);
|
||||
fs.renameSync(packageBakFile, packageFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
app.options.packages.forEach((item) => {
|
||||
packages.forEach((item) => {
|
||||
if (typeof item === 'object') {
|
||||
Object.entries(item).forEach(([key, packagePath]) => {
|
||||
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 { client = 'npm', registry = 'https://registry.npmjs.org/' } = npmConfig;
|
||||
const install = {
|
||||
npm: 'install',
|
||||
yarn: 'add',
|
||||
pnpm: 'add',
|
||||
}[client];
|
||||
try {
|
||||
const { client = 'npm', registry = 'https://registry.npmjs.org/' } = npmConfig;
|
||||
const install = {
|
||||
npm: 'install',
|
||||
yarn: 'add',
|
||||
pnpm: 'add',
|
||||
}[client];
|
||||
|
||||
const packages = Object.entries(dependencies)
|
||||
.map(([name, version]) => `${name}@${version}`)
|
||||
.join(' ');
|
||||
const packages = Object.entries(dependencies)
|
||||
.map(([name, version]) => `${name}@${version}`)
|
||||
.join(' ');
|
||||
|
||||
const command = `${client} ${install} ${packages} --registry ${registry}`;
|
||||
const command = `${client} ${install} ${packages} --registry ${registry}`;
|
||||
|
||||
execInfo(cwd);
|
||||
execInfo(command);
|
||||
execInfo(cwd);
|
||||
execInfo(command);
|
||||
|
||||
execSync(command, {
|
||||
stdio: 'inherit',
|
||||
cwd,
|
||||
});
|
||||
execSync(command, {
|
||||
stdio: 'inherit',
|
||||
cwd,
|
||||
});
|
||||
} catch (e) {
|
||||
error(e as string);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user