@icebreakers/monorepo 0.4.6 → 0.4.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -26,10 +26,10 @@
26
26
  "preinstall": "npx only-allow pnpm",
27
27
  "prepare": "husky",
28
28
  "commit": "commit",
29
- "script:init": "tsx scripts/monorepo/init.ts",
30
- "script:sync": "tsx scripts/monorepo/sync.ts",
31
- "script:clean": "tsx scripts/monorepo/clean.ts",
32
- "script:mirror": "tsx scripts/monorepo/binaryMirror.ts"
29
+ "script:init": "monorepo init",
30
+ "script:sync": "monorepo sync",
31
+ "script:clean": "monorepo clean",
32
+ "script:mirror": "monorepo mirror"
33
33
  },
34
34
  "devDependencies": {
35
35
  "@changesets/changelog-github": "^0.5.0",
@@ -39,29 +39,23 @@
39
39
  "@commitlint/prompt-cli": "^19.5.0",
40
40
  "@commitlint/types": "^19.5.0",
41
41
  "@icebreakers/eslint-config": "^0.5.1",
42
+ "@icebreakers/monorepo": "workspace:*",
42
43
  "@icebreakers/stylelint-config": "^0.0.3",
43
- "@pnpm/workspace.find-packages": "^4.0.10",
44
44
  "@types/fs-extra": "^11.0.4",
45
- "@types/get-value": "^3.0.5",
46
- "@types/git-url-parse": "^9.0.3",
47
45
  "@types/klaw": "^3.0.6",
48
46
  "@types/lint-staged": "^13.3.0",
49
47
  "@types/lodash": "^4.17.7",
50
48
  "@types/lodash-es": "^4.17.12",
51
49
  "@types/micromatch": "^4.0.9",
52
50
  "@types/node": "^22.5.5",
53
- "@types/set-value": "^4.0.3",
54
51
  "@vitest/coverage-v8": "~2.0.5",
55
52
  "ci-info": "^4.0.0",
56
- "comment-json": "^4.2.5",
57
53
  "cross-env": "^7.0.3",
58
54
  "defu": "^6.1.4",
59
55
  "eslint": "^9.10.0",
60
56
  "execa": "^9.4.0",
61
57
  "fast-glob": "^3.3.2",
62
58
  "fs-extra": "^11.2.0",
63
- "get-value": "^3.0.1",
64
- "git-url-parse": "^15.0.0",
65
59
  "husky": "^9.1.6",
66
60
  "klaw": "^4.1.0",
67
61
  "lint-staged": "^15.2.10",
@@ -72,15 +66,12 @@
72
66
  "pathe": "^1.1.2",
73
67
  "pkg-types": "^1.2.0",
74
68
  "rimraf": "^6.0.1",
75
- "set-value": "^4.1.0",
76
- "simple-git": "^3.26.0",
77
69
  "tslib": "^2.7.0",
78
70
  "tsup": "^8.3.0",
79
71
  "tsx": "^4.19.1",
80
72
  "turbo": "^2.1.2",
81
73
  "typescript": "^5.6.2",
82
74
  "unbuild": "^2.0.0",
83
- "vite-tsconfig-paths": "^5.0.1",
84
75
  "vitest": "~2.0.5",
85
76
  "yaml": "^2.5.1"
86
77
  },
@@ -1,3 +1,4 @@
1
1
  packages:
2
2
  - 'apps/*'
3
3
  - 'packages/*'
4
+ - '!**/test/**'
@@ -0,0 +1,259 @@
1
+ // src/lib.ts
2
+ import process from "node:process";
3
+ import { fileURLToPath } from "node:url";
4
+ import checkbox from "@inquirer/checkbox";
5
+ import confirm from "@inquirer/confirm";
6
+ import fs from "fs-extra";
7
+ import get2 from "get-value";
8
+ import klaw from "klaw";
9
+ import PQueue from "p-queue";
10
+ import path from "pathe";
11
+ import pc from "picocolors";
12
+ import set from "set-value";
13
+
14
+ // package.json
15
+ var name = "@icebreakers/monorepo";
16
+ var version = "0.4.8";
17
+
18
+ // src/logger.ts
19
+ import { createConsola } from "consola";
20
+ var logger = createConsola();
21
+
22
+ // src/md5.ts
23
+ import crypto from "node:crypto";
24
+ function getFileHash(data) {
25
+ const hashSum = crypto.createHash("md5");
26
+ hashSum.update(data);
27
+ return hashSum.digest("hex");
28
+ }
29
+ function isFileChanged(src, dest) {
30
+ try {
31
+ const currentHash = getFileHash(src);
32
+ const previousHash = getFileHash(dest);
33
+ return currentHash !== previousHash;
34
+ } catch (err) {
35
+ console.error("Error calculating file hash:", err);
36
+ return false;
37
+ }
38
+ }
39
+
40
+ // src/monorepo/git.ts
41
+ import get from "get-value";
42
+ import gitUrlParse from "git-url-parse";
43
+ import { simpleGit } from "simple-git";
44
+ var GitClient = class {
45
+ client;
46
+ #config;
47
+ constructor(options = {}) {
48
+ this.client = simpleGit(options);
49
+ }
50
+ listConfig() {
51
+ return this.client.listConfig();
52
+ }
53
+ async init() {
54
+ const listConfig = await this.listConfig();
55
+ this.#config = listConfig.all;
56
+ return this.#config;
57
+ }
58
+ async getConfig() {
59
+ if (this.#config) {
60
+ return this.#config;
61
+ } else {
62
+ return await this.init();
63
+ }
64
+ }
65
+ async getGitUrl() {
66
+ const config = await this.getConfig();
67
+ const x = get(config, "remote.origin.url");
68
+ if (x) {
69
+ return gitUrlParse(x);
70
+ }
71
+ }
72
+ async getRepoName() {
73
+ const url = await this.getGitUrl();
74
+ if (url) {
75
+ return `${url.owner}/${url.name}`;
76
+ }
77
+ }
78
+ async getUser() {
79
+ const config = await this.getConfig();
80
+ const name2 = get(config, "user.name");
81
+ const email = get(config, "user.email");
82
+ return {
83
+ name: name2,
84
+ email
85
+ };
86
+ }
87
+ };
88
+
89
+ // src/scripts.ts
90
+ var scripts = {
91
+ "script:init": "monorepo init",
92
+ "script:sync": "monorepo sync",
93
+ "script:clean": "monorepo clean",
94
+ "script:mirror": "monorepo mirror"
95
+ };
96
+ var scriptsEntries = Object.entries(scripts);
97
+
98
+ // src/targets.ts
99
+ function getTargets(raw) {
100
+ const list = [
101
+ ".changeset",
102
+ ".husky",
103
+ ".vscode",
104
+ ".editorconfig",
105
+ ".gitattributes",
106
+ ".gitignore",
107
+ ".npmrc",
108
+ "commitlint.config.ts",
109
+ "eslint.config.js",
110
+ "lint-staged.config.js",
111
+ "stylelint.config.js",
112
+ "package.json",
113
+ // pnpm
114
+ "pnpm-workspace.yaml",
115
+ // base tsconfig
116
+ "tsconfig.json",
117
+ // turbo
118
+ "turbo.json",
119
+ // vitest
120
+ "vitest.workspace.ts",
121
+ // #region docker
122
+ "Dockerfile",
123
+ ".dockerignore"
124
+ // #endregion
125
+ ];
126
+ if (!raw) {
127
+ list.push(".github", "LICENSE", "renovate.json", "SECURITY.md", "CODE_OF_CONDUCT.md", "CONTRIBUTING.md", "netlify.toml");
128
+ }
129
+ return list;
130
+ }
131
+
132
+ // src/utils.ts
133
+ function escapeStringRegexp(str) {
134
+ return str.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&").replace(/-/g, "\\x2d");
135
+ }
136
+ function isMatch(str, arr) {
137
+ for (const reg of arr) {
138
+ if (reg.test(str)) {
139
+ return true;
140
+ }
141
+ }
142
+ return false;
143
+ }
144
+
145
+ // src/lib.ts
146
+ var queue = new PQueue({ concurrency: 1 });
147
+ var __filename2 = fileURLToPath(import.meta.url);
148
+ var __dirname2 = path.dirname(__filename2);
149
+ var assetsDir = path.join(__dirname2, "../assets");
150
+ var cwd = process.cwd();
151
+ function setPkgJson(sourcePkgJson, targetPkgJson) {
152
+ const packageManager = get2(sourcePkgJson, "packageManager", { default: "" });
153
+ const deps = get2(sourcePkgJson, "dependencies", { default: {} });
154
+ const devDeps = get2(sourcePkgJson, "devDependencies", { default: {} });
155
+ set(targetPkgJson, "packageManager", packageManager);
156
+ Object.entries(deps).forEach((x) => {
157
+ set(targetPkgJson, `dependencies.${x[0].replaceAll(".", "\\.")}`, x[1], { preservePaths: false });
158
+ });
159
+ Object.entries(devDeps).forEach((x) => {
160
+ if (x[0] === "@icebreakers/monorepo") {
161
+ set(targetPkgJson, `devDependencies.${x[0].replaceAll(".", "\\.")}`, `^${version}`, { preservePaths: false });
162
+ } else {
163
+ set(targetPkgJson, `devDependencies.${x[0].replaceAll(".", "\\.")}`, x[1], { preservePaths: false });
164
+ }
165
+ });
166
+ for (const [k, v] of scriptsEntries) {
167
+ set(targetPkgJson, `scripts.${k}`, v);
168
+ }
169
+ }
170
+ function confirmOverwrite(filename) {
171
+ return confirm({ message: `${pc.greenBright(filename)} \u6587\u4EF6\u5185\u5BB9\u53D1\u751F\u6539\u53D8,\u662F\u5426\u8986\u76D6?`, default: false });
172
+ }
173
+ async function main(opts) {
174
+ const { outDir = "", raw, interactive } = opts;
175
+ const absOutDir = path.isAbsolute(outDir) ? outDir : path.join(cwd, outDir);
176
+ const gitClient = new GitClient({
177
+ baseDir: cwd
178
+ });
179
+ const repoName = await gitClient.getRepoName();
180
+ let targets = getTargets(raw);
181
+ if (interactive) {
182
+ targets = await checkbox({
183
+ message: "\u9009\u62E9\u4F60\u9700\u8981\u7684\u6587\u4EF6",
184
+ choices: targets.map((x) => {
185
+ return {
186
+ value: x,
187
+ checked: true
188
+ };
189
+ })
190
+ });
191
+ }
192
+ const regexpArr = targets.map((x) => {
193
+ return new RegExp(`^${escapeStringRegexp(x)}`);
194
+ });
195
+ for await (const file of klaw(assetsDir, {
196
+ filter(p) {
197
+ const str = path.relative(assetsDir, p);
198
+ return isMatch(str, regexpArr);
199
+ }
200
+ })) {
201
+ await queue.add(async () => {
202
+ if (file.stats.isFile()) {
203
+ const relPath = path.relative(assetsDir, file.path);
204
+ const targetPath = path.resolve(absOutDir, relPath);
205
+ const targetIsExisted = await fs.exists(targetPath);
206
+ async function overwriteOrCopy(target) {
207
+ let isOverwrite = true;
208
+ if (targetIsExisted) {
209
+ const src = await fs.readFile(file.path);
210
+ const dest = target ?? await fs.readFile(targetPath);
211
+ if (await isFileChanged(src, dest)) {
212
+ isOverwrite = await confirmOverwrite(relPath);
213
+ }
214
+ }
215
+ return isOverwrite;
216
+ }
217
+ if (relPath === "package.json") {
218
+ const sourcePath = file.path;
219
+ if (targetIsExisted) {
220
+ const sourcePkgJson = await fs.readJson(sourcePath);
221
+ const targetPkgJson = await fs.readJson(targetPath);
222
+ setPkgJson(sourcePkgJson, targetPkgJson);
223
+ const data = JSON.stringify(targetPkgJson, void 0, 2);
224
+ await fs.writeFile(targetPath, `${data}
225
+ `, "utf8");
226
+ logger.success(targetPath);
227
+ }
228
+ } else if (relPath === ".changeset/config.json" && repoName) {
229
+ const changesetJson = await fs.readJson(file.path);
230
+ set(changesetJson, "changelog.1.repo", repoName);
231
+ const data = JSON.stringify(changesetJson, void 0, 2);
232
+ if (await overwriteOrCopy(data)) {
233
+ await fs.ensureDir(path.dirname(targetPath));
234
+ await fs.writeFile(targetPath, `${data}
235
+ `, "utf8");
236
+ logger.success(targetPath);
237
+ }
238
+ } else {
239
+ if (await overwriteOrCopy()) {
240
+ await fs.copy(
241
+ file.path,
242
+ targetPath
243
+ );
244
+ logger.success(targetPath);
245
+ }
246
+ }
247
+ }
248
+ });
249
+ }
250
+ }
251
+
252
+ export {
253
+ name,
254
+ version,
255
+ logger,
256
+ GitClient,
257
+ setPkgJson,
258
+ main
259
+ };