From fa83e8f47a10a5ad7ed1ac5337d8a2d20c556df9 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Sun, 2 Jan 2022 05:22:42 +0800 Subject: [PATCH] refactor: code splitting --- packages/@yarn-tool/fix-all/lib/root/index.js | 8 ++++ packages/@yarn-tool/fix-all/lib/root/index.ts | 9 ++++ .../pkg-entry-util/lib/preset/dummy.d.ts | 1 + .../pkg-entry-util/lib/preset/dummy.js | 29 ++++++++++++ .../pkg-entry-util/lib/preset/dummy.ts | 27 ++++++++++++ .../lib/preset/ws-root-scripts.d.ts | 27 ++++++++++++ .../lib/preset/ws-root-scripts.js | 34 ++++++++++++++ .../lib/preset/ws-root-scripts.ts | 30 +++++++++++++ packages/create-yarn-workspaces/lib/index.js | 44 ++----------------- packages/create-yarn-workspaces/lib/index.ts | 43 ++---------------- packages/create-yarn-workspaces/package.json | 1 + packages/npm-init2/index.js | 19 +------- packages/npm-init2/index.ts | 19 +------- packages/npm-init2/package.json | 1 + 14 files changed, 179 insertions(+), 113 deletions(-) create mode 100644 packages/@yarn-tool/pkg-entry-util/lib/preset/dummy.d.ts create mode 100644 packages/@yarn-tool/pkg-entry-util/lib/preset/dummy.js create mode 100644 packages/@yarn-tool/pkg-entry-util/lib/preset/dummy.ts create mode 100644 packages/@yarn-tool/pkg-entry-util/lib/preset/ws-root-scripts.d.ts create mode 100644 packages/@yarn-tool/pkg-entry-util/lib/preset/ws-root-scripts.js create mode 100644 packages/@yarn-tool/pkg-entry-util/lib/preset/ws-root-scripts.ts diff --git a/packages/@yarn-tool/fix-all/lib/root/index.js b/packages/@yarn-tool/fix-all/lib/root/index.js index fe39291f6..075560494 100644 --- a/packages/@yarn-tool/fix-all/lib/root/index.js +++ b/packages/@yarn-tool/fix-all/lib/root/index.js @@ -7,6 +7,8 @@ const pkg_git_info_1 = require("@yarn-tool/pkg-git-info"); const npm_package_json_loader_1 = require("npm-package-json-loader"); const ws_scope_1 = require("@yarn-tool/ws-scope"); const sort_package_json3_1 = require("sort-package-json3"); +const ws_root_scripts_1 = require("@yarn-tool/pkg-entry-util/lib/preset/ws-root-scripts"); +const dummy_1 = require("@yarn-tool/pkg-entry-util/lib/preset/dummy"); function _fixRoot(options) { let { rootData, branch, overwriteHostedGitInfo, hostedGitInfo, targetDir } = options; const root_file_package_json = (0, upath2_1.join)(targetDir, 'package.json'); @@ -43,6 +45,12 @@ function _fixWsRoot(options) { ...options, targetDir: options.rootData.ws, }); + Object.entries((0, dummy_1.fillDummyScripts)((0, ws_root_scripts_1.defaultWorkspaceRootScripts)())).forEach(([key, value]) => { + var _a; + var _b; + (_a = (_b = runtime.root_pkg_json.data.scripts)[key]) !== null && _a !== void 0 ? _a : (_b[key] = value); + }); + runtime.root_pkg_json.write(); let wss = new ws_scope_1.WorkspacesScope(runtime.rootData.ws); wss.syncValue(); wss.save(); diff --git a/packages/@yarn-tool/fix-all/lib/root/index.ts b/packages/@yarn-tool/fix-all/lib/root/index.ts index 82272c084..929707efa 100644 --- a/packages/@yarn-tool/fix-all/lib/root/index.ts +++ b/packages/@yarn-tool/fix-all/lib/root/index.ts @@ -5,6 +5,8 @@ import { npmHostedGitInfoLazy } from '@yarn-tool/pkg-git-info'; import { PackageJsonLoader } from 'npm-package-json-loader'; import { WorkspacesScope } from '@yarn-tool/ws-scope'; import { sortPackageJson } from 'sort-package-json3'; +import { defaultWorkspaceRootScripts } from '@yarn-tool/pkg-entry-util/lib/preset/ws-root-scripts'; +import { fillDummyScripts } from '@yarn-tool/pkg-entry-util/lib/preset/dummy'; export function _fixRoot(options: Required) { @@ -54,6 +56,13 @@ export function _fixWsRoot(options: ITSRequiredPick + { + runtime.root_pkg_json.data.scripts[key] ??= value; + }); + + runtime.root_pkg_json.write(); + let wss = new WorkspacesScope(runtime.rootData.ws); wss.syncValue(); wss.save(); diff --git a/packages/@yarn-tool/pkg-entry-util/lib/preset/dummy.d.ts b/packages/@yarn-tool/pkg-entry-util/lib/preset/dummy.d.ts new file mode 100644 index 000000000..37166e9f6 --- /dev/null +++ b/packages/@yarn-tool/pkg-entry-util/lib/preset/dummy.d.ts @@ -0,0 +1 @@ +export declare function fillDummyScripts>(scripts?: T, prefix?: string): T; diff --git a/packages/@yarn-tool/pkg-entry-util/lib/preset/dummy.js b/packages/@yarn-tool/pkg-entry-util/lib/preset/dummy.js new file mode 100644 index 000000000..90ab1e35d --- /dev/null +++ b/packages/@yarn-tool/pkg-entry-util/lib/preset/dummy.js @@ -0,0 +1,29 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.fillDummyScripts = void 0; +function fillDummyScripts(scripts, prefix) { + // @ts-ignore + scripts !== null && scripts !== void 0 ? scripts : (scripts = {}); + let text = `echo ${prefix !== null && prefix !== void 0 ? prefix : ''}`.trim(); + [ + 'preversion', + 'version', + 'prepublishOnly', + 'postversion', + 'publish', + 'prepublish', + 'postpublish', + 'postpublishOnly', + 'prepare', + 'prepack', + 'pack', + 'postpack', + ].forEach(k => { + var _a; + // @ts-ignore + (_a = scripts[k]) !== null && _a !== void 0 ? _a : (scripts[k] = `${text} ${k}`); + }); + return scripts; +} +exports.fillDummyScripts = fillDummyScripts; +//# sourceMappingURL=dummy.js.map \ No newline at end of file diff --git a/packages/@yarn-tool/pkg-entry-util/lib/preset/dummy.ts b/packages/@yarn-tool/pkg-entry-util/lib/preset/dummy.ts new file mode 100644 index 000000000..d370a3c1e --- /dev/null +++ b/packages/@yarn-tool/pkg-entry-util/lib/preset/dummy.ts @@ -0,0 +1,27 @@ +export function fillDummyScripts>(scripts?: T, prefix?: string) +{ + // @ts-ignore + scripts ??= {}; + + let text = `echo ${prefix ?? ''}`.trim(); + + ([ + 'preversion', + 'version', + 'prepublishOnly', + 'postversion', + 'publish', + 'prepublish', + 'postpublish', + 'postpublishOnly', + 'prepare', + 'prepack', + 'pack', + 'postpack', + ] as const).forEach(k => + { + // @ts-ignore + scripts[k] ??= `${text} ${k}` + }); + return scripts; +} diff --git a/packages/@yarn-tool/pkg-entry-util/lib/preset/ws-root-scripts.d.ts b/packages/@yarn-tool/pkg-entry-util/lib/preset/ws-root-scripts.d.ts new file mode 100644 index 000000000..2e3aa01c2 --- /dev/null +++ b/packages/@yarn-tool/pkg-entry-util/lib/preset/ws-root-scripts.d.ts @@ -0,0 +1,27 @@ +export declare function defaultWorkspaceRootScripts(): { + "test:all": string; + "build:all": string; + "review:all": string; + "coverage:all": string; + "lint:all": string; + preversion: string; + "lerna:publish": string; + "lerna:publish:yes": string; + "prepublishOnly:root": string; + "prepublishOnly:lockfile": string; + "prepublishOnly:check-bin": string; + "prepare:fix-ws-links": string; + "prepublishOnly:update": string; + ncu: string; + "ncu:root": string; + "ncu:ws": string; + "sort-package-json": string; + "sort-package-json:root": string; + "sort-package-json:ws": string; + postpublishOnly: string; + "postpublishOnly:ws-root-changelog": string; + test: string; + "install:reset-lockfile": string; + "ws:fix-all": string; + "tsc:showConfig": string; +}; diff --git a/packages/@yarn-tool/pkg-entry-util/lib/preset/ws-root-scripts.js b/packages/@yarn-tool/pkg-entry-util/lib/preset/ws-root-scripts.js new file mode 100644 index 000000000..6d5fb7382 --- /dev/null +++ b/packages/@yarn-tool/pkg-entry-util/lib/preset/ws-root-scripts.js @@ -0,0 +1,34 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.defaultWorkspaceRootScripts = void 0; +function defaultWorkspaceRootScripts() { + return { + "test:all": "lerna run test --concurrency 1", + "build:all": "lerna run build --concurrency 1", + "review:all": "lerna run review --concurrency 1", + "coverage:all": "lerna run coverage --concurrency 1", + "lint:all": "lerna run lint --concurrency 1", + "preversion": "yt ws run test", + "lerna:publish": "yarn run prepublishOnly:root && lerna publish && yarn run postpublishOnly", + "lerna:publish:yes": "yarn run prepublishOnly:root && lerna publish --yes --bump patch && yarn run postpublishOnly", + "prepublishOnly:root": "yarn run prepublishOnly:check-bin && yarn run prepare:fix-ws-links", + "prepublishOnly:lockfile": "ynpx --quiet sync-lockfile", + "prepublishOnly:check-bin": "ynpx --quiet @yarn-tool/check-pkg-bin", + "prepare:fix-ws-links": "ynpx --quiet @yarn-tool/fix-ws-links", + "prepublishOnly:update": "yarn run ncu && yarn run sort-package-json", + "ncu": "yarn run ncu:root && yarn run ncu:ws", + "ncu:root": "yarn-tool ncu -u", + "ncu:ws": "yarn-tool ws exec yarn-tool ncu -- -u", + "sort-package-json": "yarn run sort-package-json:root && yarn run sort-package-json:ws", + "sort-package-json:root": "yarn-tool sort", + "sort-package-json:ws": "yarn-tool ws sort", + "postpublishOnly": "yarn run postpublishOnly:ws-root-changelog & echo postpublishOnly", + "postpublishOnly:ws-root-changelog": "ynpx ws-root-changelog & git add ./CHANGELOG.md & git commit ./CHANGELOG.md -m \"chore(changelog): update changelog toc in workspaces root\" & echo update changelog toc in workspaces root", + "test": "yarn-tool ws run test", + "install:reset-lockfile": "yarn-tool install --reset-lockfile", + "ws:fix-all": "yarn-tool fix-all --overwriteHostedGitInfo", + "tsc:showConfig": "ynpx get-current-tsconfig -p", + }; +} +exports.defaultWorkspaceRootScripts = defaultWorkspaceRootScripts; +//# sourceMappingURL=ws-root-scripts.js.map \ No newline at end of file diff --git a/packages/@yarn-tool/pkg-entry-util/lib/preset/ws-root-scripts.ts b/packages/@yarn-tool/pkg-entry-util/lib/preset/ws-root-scripts.ts new file mode 100644 index 000000000..f85c87049 --- /dev/null +++ b/packages/@yarn-tool/pkg-entry-util/lib/preset/ws-root-scripts.ts @@ -0,0 +1,30 @@ +export function defaultWorkspaceRootScripts() +{ + return { + "test:all": "lerna run test --concurrency 1", + "build:all": "lerna run build --concurrency 1", + "review:all": "lerna run review --concurrency 1", + "coverage:all": "lerna run coverage --concurrency 1", + "lint:all": "lerna run lint --concurrency 1", + "preversion": "yt ws run test", + "lerna:publish": "yarn run prepublishOnly:root && lerna publish && yarn run postpublishOnly", + "lerna:publish:yes": "yarn run prepublishOnly:root && lerna publish --yes --bump patch && yarn run postpublishOnly", + "prepublishOnly:root": "yarn run prepublishOnly:check-bin && yarn run prepare:fix-ws-links", + "prepublishOnly:lockfile": "ynpx --quiet sync-lockfile", + "prepublishOnly:check-bin": "ynpx --quiet @yarn-tool/check-pkg-bin", + "prepare:fix-ws-links": "ynpx --quiet @yarn-tool/fix-ws-links", + "prepublishOnly:update": "yarn run ncu && yarn run sort-package-json", + "ncu": "yarn run ncu:root && yarn run ncu:ws", + "ncu:root": "yarn-tool ncu -u", + "ncu:ws": "yarn-tool ws exec yarn-tool ncu -- -u", + "sort-package-json": "yarn run sort-package-json:root && yarn run sort-package-json:ws", + "sort-package-json:root": "yarn-tool sort", + "sort-package-json:ws": "yarn-tool ws sort", + "postpublishOnly": "yarn run postpublishOnly:ws-root-changelog & echo postpublishOnly", + "postpublishOnly:ws-root-changelog": "ynpx ws-root-changelog & git add ./CHANGELOG.md & git commit ./CHANGELOG.md -m \"chore(changelog): update changelog toc in workspaces root\" & echo update changelog toc in workspaces root", + "test": "yarn-tool ws run test", + "install:reset-lockfile": "yarn-tool install --reset-lockfile", + "ws:fix-all":"yarn-tool fix-all --overwriteHostedGitInfo", + "tsc:showConfig": "ynpx get-current-tsconfig -p", + } +} diff --git a/packages/create-yarn-workspaces/lib/index.js b/packages/create-yarn-workspaces/lib/index.js index 31a94bf51..d3c6ab2e2 100644 --- a/packages/create-yarn-workspaces/lib/index.js +++ b/packages/create-yarn-workspaces/lib/index.js @@ -1,6 +1,8 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getDefaultTsconfig = exports.getDefaultPackageJson = void 0; +const ws_root_scripts_1 = require("@yarn-tool/pkg-entry-util/lib/preset/ws-root-scripts"); +const dummy_1 = require("@yarn-tool/pkg-entry-util/lib/preset/dummy"); function getDefaultPackageJson(name) { let json = { "name": name, @@ -13,32 +15,7 @@ function getDefaultPackageJson(name) { keywords: [ "create-by-yarn-tool", ], - "scripts": { - "test:all": "lerna run test --concurrency 1", - "build:all": "lerna run build --concurrency 1", - "review:all": "lerna run review --concurrency 1", - "coverage:all": "lerna run coverage --concurrency 1", - "lint:all": "lerna run lint --concurrency 1", - "preversion": "yt ws run test", - "lerna:publish": "yarn run prepublishOnly:root && lerna publish && yarn run postpublishOnly", - "lerna:publish:yes": "yarn run prepublishOnly:root && lerna publish --yes --bump patch && yarn run postpublishOnly", - "prepublishOnly:root": "yarn run prepublishOnly:check-bin && yarn run prepare:fix-ws-links", - "prepublishOnly:lockfile": "ynpx --quiet sync-lockfile", - "prepublishOnly:check-bin": "ynpx --quiet @yarn-tool/check-pkg-bin", - "prepare:fix-ws-links": "ynpx --quiet @yarn-tool/fix-ws-links", - "prepublishOnly:update": "yarn run ncu && yarn run sort-package-json", - "ncu": "yarn run ncu:root && yarn run ncu:ws", - "ncu:root": "yarn-tool ncu -u", - "ncu:ws": "yarn-tool ws exec yarn-tool ncu -- -u", - "sort-package-json": "yarn run sort-package-json:root && yarn run sort-package-json:ws", - "sort-package-json:root": "yarn-tool sort", - "sort-package-json:ws": "yarn-tool ws sort", - "postpublishOnly": "yarn run postpublishOnly:ws-root-changelog & echo postpublishOnly", - "postpublishOnly:ws-root-changelog": "ynpx ws-root-changelog & git add ./CHANGELOG.md & git commit ./CHANGELOG.md -m \"chore(changelog): update changelog toc in workspaces root\" & echo update changelog toc in workspaces root", - "test": "yarn-tool ws run test", - "install:reset-lockfile": "yarn-tool install --reset-lockfile", - "tsc:showConfig": "ynpx get-current-tsconfig -p", - }, + "scripts": (0, ws_root_scripts_1.defaultWorkspaceRootScripts)(), "dependencies": { "ts-type": "^1.2.32", "tslib": "^2.3.0", @@ -60,20 +37,7 @@ function getDefaultPackageJson(name) { }, "resolutions": {}, }; - [ - 'preversion', - 'version', - 'prepublishOnly', - 'postversion', - 'publish', - 'prepublish', - 'postpublish', - 'postpublishOnly', - ].forEach(k => { - var _a; - var _b; - (_a = (_b = json.scripts)[k]) !== null && _a !== void 0 ? _a : (_b[k] = `echo workspaces ${k}`); - }); + (0, dummy_1.fillDummyScripts)(json.scripts, 'workspaces'); return json; } exports.getDefaultPackageJson = getDefaultPackageJson; diff --git a/packages/create-yarn-workspaces/lib/index.ts b/packages/create-yarn-workspaces/lib/index.ts index ab7edc04f..7b05d5bbf 100644 --- a/packages/create-yarn-workspaces/lib/index.ts +++ b/packages/create-yarn-workspaces/lib/index.ts @@ -1,4 +1,6 @@ import { IPackageJson } from '@ts-type/package-dts/package-json'; +import { defaultWorkspaceRootScripts } from '@yarn-tool/pkg-entry-util/lib/preset/ws-root-scripts'; +import { fillDummyScripts } from '@yarn-tool/pkg-entry-util/lib/preset/dummy'; export function getDefaultPackageJson(name?: string): IPackageJson { @@ -13,32 +15,7 @@ export function getDefaultPackageJson(name?: string): IPackageJson keywords: [ "create-by-yarn-tool", ], - "scripts": { - "test:all": "lerna run test --concurrency 1", - "build:all": "lerna run build --concurrency 1", - "review:all": "lerna run review --concurrency 1", - "coverage:all": "lerna run coverage --concurrency 1", - "lint:all": "lerna run lint --concurrency 1", - "preversion": "yt ws run test", - "lerna:publish": "yarn run prepublishOnly:root && lerna publish && yarn run postpublishOnly", - "lerna:publish:yes": "yarn run prepublishOnly:root && lerna publish --yes --bump patch && yarn run postpublishOnly", - "prepublishOnly:root": "yarn run prepublishOnly:check-bin && yarn run prepare:fix-ws-links", - "prepublishOnly:lockfile": "ynpx --quiet sync-lockfile", - "prepublishOnly:check-bin": "ynpx --quiet @yarn-tool/check-pkg-bin", - "prepare:fix-ws-links": "ynpx --quiet @yarn-tool/fix-ws-links", - "prepublishOnly:update": "yarn run ncu && yarn run sort-package-json", - "ncu": "yarn run ncu:root && yarn run ncu:ws", - "ncu:root": "yarn-tool ncu -u", - "ncu:ws": "yarn-tool ws exec yarn-tool ncu -- -u", - "sort-package-json": "yarn run sort-package-json:root && yarn run sort-package-json:ws", - "sort-package-json:root": "yarn-tool sort", - "sort-package-json:ws": "yarn-tool ws sort", - "postpublishOnly": "yarn run postpublishOnly:ws-root-changelog & echo postpublishOnly", - "postpublishOnly:ws-root-changelog": "ynpx ws-root-changelog & git add ./CHANGELOG.md & git commit ./CHANGELOG.md -m \"chore(changelog): update changelog toc in workspaces root\" & echo update changelog toc in workspaces root", - "test": "yarn-tool ws run test", - "install:reset-lockfile": "yarn-tool install --reset-lockfile", - "tsc:showConfig": "ynpx get-current-tsconfig -p", - }, + "scripts": defaultWorkspaceRootScripts(), "dependencies": { "ts-type": "^1.2.32", "tslib": "^2.3.0", @@ -61,19 +38,7 @@ export function getDefaultPackageJson(name?: string): IPackageJson "resolutions": {}, } as any; - [ - 'preversion', - 'version', - 'prepublishOnly', - 'postversion', - 'publish', - 'prepublish', - 'postpublish', - 'postpublishOnly', - ].forEach(k => - { - json.scripts[k] ??= `echo workspaces ${k}` - }); + fillDummyScripts(json.scripts, 'workspaces'); return json; } diff --git a/packages/create-yarn-workspaces/package.json b/packages/create-yarn-workspaces/package.json index 9538553b9..d8b36955c 100644 --- a/packages/create-yarn-workspaces/package.json +++ b/packages/create-yarn-workspaces/package.json @@ -49,6 +49,7 @@ "dependencies": { "@ts-type/package-dts": "^1.0.56", "@yarn-tool/find-root": "^1.0.60", + "@yarn-tool/pkg-entry-util": "^1.0.3", "@yarn-tool/static-file": "^1.0.87", "debug-color2": "^1.2.7", "find-yarn-workspace-root2": "^1.2.36", diff --git a/packages/npm-init2/index.js b/packages/npm-init2/index.js index f926491b4..bcead9a92 100644 --- a/packages/npm-init2/index.js +++ b/packages/npm-init2/index.js @@ -27,6 +27,7 @@ const logger_1 = require("debug-color2/logger"); const nameExistsInWorkspaces_1 = require("ws-pkg-list/lib/nameExistsInWorkspaces"); const pkg_hosted_info_1 = require("@yarn-tool/pkg-hosted-info"); const index_1 = require("@yarn-tool/setup-module-env/lib/preset/tsdx/index"); +const dummy_1 = require("@yarn-tool/pkg-entry-util/lib/preset/dummy"); //updateNotifier(__dirname); // avoid buf for idea logger_1.consoleLogger.length; @@ -156,23 +157,7 @@ if (!cp.error) { "test": `echo "Error: no test specified"`, "tsc:showConfig": "ynpx get-current-tsconfig -p", }; - [ - 'preversion', - 'version', - 'prepublishOnly', - 'postversion', - 'publish', - 'prepare', - 'prepack', - 'pack', - 'postpack', - 'prepublish', - 'postpublish', - 'postpublishOnly', - ].forEach(k => { - var _a; - (_a = sharedScript[k]) !== null && _a !== void 0 ? _a : (sharedScript[k] = `echo ${k}`); - }); + (0, dummy_1.fillDummyScripts)(sharedScript); let preScripts = ["echo preversion"]; /* if (rootData.isRoot || rootData.hasWorkspace && !wsProject.manifest.scripts?.['prepublishOnly:check-bin']) diff --git a/packages/npm-init2/index.ts b/packages/npm-init2/index.ts index 457b3149c..af5038224 100644 --- a/packages/npm-init2/index.ts +++ b/packages/npm-init2/index.ts @@ -25,6 +25,7 @@ import { consoleLogger as console } from 'debug-color2/logger'; import { nameExistsInWorkspaces } from 'ws-pkg-list/lib/nameExistsInWorkspaces'; import { fillPkgHostedInfo } from '@yarn-tool/pkg-hosted-info'; import { setup as setupTsdx } from '@yarn-tool/setup-module-env/lib/preset/tsdx/index'; +import { fillDummyScripts } from '@yarn-tool/pkg-entry-util/lib/preset/dummy'; //updateNotifier(__dirname); @@ -214,23 +215,7 @@ if (!cp.error) "tsc:showConfig": "ynpx get-current-tsconfig -p", }; - [ - 'preversion', - 'version', - 'prepublishOnly', - 'postversion', - 'publish', - 'prepare', - 'prepack', - 'pack', - 'postpack', - 'prepublish', - 'postpublish', - 'postpublishOnly', - ].forEach(k => - { - sharedScript[k] ??= `echo ${k}` - }); + fillDummyScripts(sharedScript); let preScripts: string[] = ["echo preversion"]; diff --git a/packages/npm-init2/package.json b/packages/npm-init2/package.json index e3024de5a..db98c0e6e 100644 --- a/packages/npm-init2/package.json +++ b/packages/npm-init2/package.json @@ -52,6 +52,7 @@ "@yarn-tool/init-path": "^1.0.19", "@yarn-tool/is-builtin-module": "^1.0.11", "@yarn-tool/node-modules-link": "^1.0.38", + "@yarn-tool/pkg-entry-util": "^1.0.3", "@yarn-tool/pkg-git-info": "^1.0.24", "@yarn-tool/pkg-hosted-info": "^1.0.10", "@yarn-tool/setup-module-env": "^1.0.5",