feat: update ev cli
This commit is contained in:
@@ -7,11 +7,18 @@ import { getConfig } from '@/module/get-config.ts';
|
||||
import fs from 'fs';
|
||||
import inquirer from 'inquirer';
|
||||
import { checkPnpm } from '@/uitls/npm.ts';
|
||||
|
||||
const parseIfJson = (str: string) => {
|
||||
try {
|
||||
return JSON.parse(str);
|
||||
} catch (e) {
|
||||
return {};
|
||||
}
|
||||
};
|
||||
const command = new Command('npm').description('npm command show publish and set .npmrc').action(async (options) => {});
|
||||
const publish = new Command('publish')
|
||||
.argument('[registry]')
|
||||
.option('-p --proxy', 'proxy')
|
||||
.option('-t, --tag', 'tag')
|
||||
.description('publish npm')
|
||||
.action(async (registry, options) => {
|
||||
const answer = await inquirer.prompt([
|
||||
@@ -53,15 +60,12 @@ const publish = new Command('publish')
|
||||
switch (registry) {
|
||||
case 'me':
|
||||
cmd = 'npm publish --registry https://npm.xiongxiao.me';
|
||||
console.log(chalk.green(cmd));
|
||||
break;
|
||||
case 'npm':
|
||||
cmd = 'npm publish --registry https://registry.npmjs.org';
|
||||
console.log(chalk.green(cmd));
|
||||
break;
|
||||
default:
|
||||
cmd = 'npm publish --registry https://npm.xiongxiao.me';
|
||||
console.log(chalk.green(cmd));
|
||||
break;
|
||||
}
|
||||
if (fileIsExist(packageJson)) {
|
||||
@@ -72,6 +76,20 @@ const publish = new Command('publish')
|
||||
[key]: config[key],
|
||||
};
|
||||
}, {});
|
||||
const pkg = fs.readFileSync(packageJson, 'utf-8');
|
||||
const pkgJson = parseIfJson(pkg);
|
||||
const version = pkgJson?.version as string;
|
||||
if (version && options?.tag) {
|
||||
let tag = String(version).split('-')[1] || '';
|
||||
if (tag) {
|
||||
if (tag.includes('.')) {
|
||||
tag = tag.split('.')[0];
|
||||
}
|
||||
cmd = `${cmd} --tag ${tag}`;
|
||||
}
|
||||
}
|
||||
console.log(chalk.green(cmd));
|
||||
|
||||
const child = spawn(cmd, {
|
||||
shell: true,
|
||||
cwd: execPath,
|
||||
|
||||
@@ -94,6 +94,29 @@ const checkDelete = async (opts?: { force?: boolean; dir?: string; yes?: boolean
|
||||
}
|
||||
}
|
||||
};
|
||||
export const rewritePkg = (packagePath: string, pkg: Package) => {
|
||||
const readJsonFile = (filePath: string) => {
|
||||
try {
|
||||
return JSON.parse(fs.readFileSync(filePath, 'utf-8'));
|
||||
} catch (error) {
|
||||
return {};
|
||||
}
|
||||
};
|
||||
try {
|
||||
const dirname = path.dirname(packagePath);
|
||||
if (!fs.existsSync(dirname)) {
|
||||
fs.mkdirSync(dirname, { recursive: true });
|
||||
}
|
||||
const json = readJsonFile(packagePath);
|
||||
json.id = pkg?.id;
|
||||
json.appInfo = pkg;
|
||||
|
||||
fs.writeFileSync(packagePath, JSON.stringify(json, null, 2));
|
||||
} catch (error) {
|
||||
fs.writeFileSync(packagePath, JSON.stringify({ appInfo: pkg, id: pkg?.id }, null, 2));
|
||||
}
|
||||
return pkg;
|
||||
};
|
||||
type InstallAppOpts = {
|
||||
appDir?: string;
|
||||
kevisualUrl?: string;
|
||||
@@ -118,7 +141,6 @@ export const installApp = async (app: Package, opts: InstallAppOpts = {}) => {
|
||||
try {
|
||||
let files = _app.data.files || [];
|
||||
const version = _app.version;
|
||||
let hasPackage = false;
|
||||
const user = _app.user;
|
||||
const key = _app.key;
|
||||
const downloadDirPath = appType === 'web' ? path.join(appDir, user, key) : path.join(appDir);
|
||||
@@ -128,9 +150,6 @@ export const installApp = async (app: Package, opts: InstallAppOpts = {}) => {
|
||||
.filter((file: any) => file?.path)
|
||||
.map((file: any) => {
|
||||
const name = file?.name || '';
|
||||
if (name.startsWith('package.json')) {
|
||||
hasPackage = true;
|
||||
}
|
||||
const noVersionPath = file.path.replace(`/${version}`, '');
|
||||
let downloadPath = noVersionPath;
|
||||
let downloadUrl = '';
|
||||
@@ -150,15 +169,7 @@ export const installApp = async (app: Package, opts: InstallAppOpts = {}) => {
|
||||
});
|
||||
const downloadTasks: DownloadTask[] = downFiles as any;
|
||||
console.log('downloadTasks', downloadTasks);
|
||||
if (!hasPackage) {
|
||||
console.log('没有package.json文件, 生成一个');
|
||||
const pkg = { ..._app };
|
||||
if (pkg.data) {
|
||||
delete pkg.data.permission;
|
||||
}
|
||||
fs.writeFileSync(packagePath, JSON.stringify(pkg, null, 2));
|
||||
}
|
||||
// return;
|
||||
|
||||
for (const file of downloadTasks) {
|
||||
const downloadPath = file.downloadPath;
|
||||
const downloadUrl = file.downloadUrl;
|
||||
@@ -186,6 +197,7 @@ export const installApp = async (app: Package, opts: InstallAppOpts = {}) => {
|
||||
// fs.writeFileSync(path.join(appDir, `${user}/${key}/index.html`), JSON.stringify(app, null, 2));
|
||||
// }
|
||||
_app.data.files = files;
|
||||
rewritePkg(packagePath, _app);
|
||||
return {
|
||||
code: 200,
|
||||
data: _app,
|
||||
|
||||
Reference in New Issue
Block a user