Skip to content

Commit

Permalink
feat: cli for caching packages
Browse files Browse the repository at this point in the history
  • Loading branch information
linbudu599 committed Mar 15, 2022
1 parent 9d1bf23 commit af73567
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 30 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"docs:serve": "vitepress serve docs",
"ci:version": "changeset version && pnpm install --no-frozen-lockfile && git add .",
"ci:publish": "pnpm build:all && pnpm changeset publish",
"postinstall": "ts-node -P scripts/tsconfig.script.json scripts/postinstall.ts"
"remind": "ts-node -P scripts/tsconfig.script.json scripts/postinstall-reminder.ts",
"postinstall": "ts-node -P scripts/tsconfig.script.json scripts/cli.ts cache && pnpm remind"
},
"repository": {
"type": "git",
Expand Down
37 changes: 37 additions & 0 deletions scripts/cache-package.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { CAC } from 'cac';
import fs from 'fs-extra';
import enquirer from 'enquirer';

import chalk from 'chalk';
import { CLIUtils } from './utils';
import consola from 'consola';

export default function useCachePackage(cli: CAC) {
cli
.command('cache', 'cache current workspace packages to cache directory')
.action(async () => {
fs.ensureDirSync(CLIUtils.resolvedPackageRootDir);
const existPackages = CLIUtils.existPackages;

for (const p of existPackages) {
const projectSrcPath = CLIUtils.resolvePackageDir(p);

const projectDestPath = CLIUtils.resolveCachePackageDir(p);

if (fs.existsSync(projectDestPath)) continue;

fs.copySync(projectSrcPath, projectDestPath, {
recursive: true,
filter: (src, dest) => {
const filtered = ['node_modules', 'dist', 'tmp'].every(
(pattern) => !src.includes(pattern)
);

return filtered;
},
});

consola.success(`Package ${chalk.green(p)} cached.`);
}
});
}
2 changes: 2 additions & 0 deletions scripts/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import useInitWorkspaceAfterInstall from './init-workspace';
import useResetWorkspacePackages from './reset-workspace';
import useCreateSimplePackage from './create-package';
import useCopyPackage from './copy-package';
import useCachePackage from './cache-package';

const cli = cac('LinbuduLab-Starter');

Expand All @@ -18,6 +19,7 @@ useInitWorkspaceAfterInstall(cli);
useResetWorkspacePackages(cli);
useCreateSimplePackage(cli);
useCopyPackage(cli);
useCachePackage(cli);

cli.help();
cli.parse();
29 changes: 2 additions & 27 deletions scripts/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,5 @@
import fs from 'fs-extra';
import path from 'path';

import { CLIUtils, Constants } from './utils';

export class CLIHooks {
public static pre() {
fs.ensureDirSync(CLIUtils.resolvedPackageRootDir);
const existPackages = CLIUtils.existPackages;

for (const p of existPackages.slice(0)) {
const projectSrcPath = CLIUtils.resolvePackageDir(p);

const projectDestPath = CLIUtils.resolveCachePackageDir(p);

if (fs.existsSync(projectDestPath)) continue;

fs.copySync(projectSrcPath, projectDestPath, {
recursive: true,
filter: (src, dest) => {
const filtered = ['node_modules', 'dist', 'tmp'].every(
(pattern) => !src.includes(pattern)
);
public static pre() {}

return filtered;
},
});
}
}
public static post() {}
}
4 changes: 2 additions & 2 deletions scripts/postinstall.ts → scripts/postinstall-reminder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import consola from 'consola';
console.log('');
consola.success(
`Your workspace has been successfully created, now run ${chalk.bold.cyan(
'\n\npnpm cli init\n\n'
)}to initialize workspace projects!`
'>>> pnpm cli init <<<'
)} to initialize workspace projects!`
);

0 comments on commit af73567

Please sign in to comment.