@icebreakers/monorepo 0.6.3 → 0.6.5

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.
@@ -1,279 +0,0 @@
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.6.3";
17
-
18
- // src/logger.ts
19
- import { createConsola } from "consola";
20
- var logger = createConsola();
21
-
22
- // src/monorepo/git.ts
23
- import get from "get-value";
24
- import gitUrlParse from "git-url-parse";
25
- import { simpleGit } from "simple-git";
26
- var GitClient = class {
27
- client;
28
- #config;
29
- constructor(options = {}) {
30
- this.client = simpleGit(options);
31
- }
32
- listConfig() {
33
- return this.client.listConfig();
34
- }
35
- async init() {
36
- const listConfig = await this.listConfig();
37
- this.#config = listConfig.all;
38
- return this.#config;
39
- }
40
- async getConfig() {
41
- if (this.#config) {
42
- return this.#config;
43
- } else {
44
- return await this.init();
45
- }
46
- }
47
- async getGitUrl() {
48
- const config = await this.getConfig();
49
- const x = get(config, "remote.origin.url");
50
- if (x) {
51
- return gitUrlParse(x);
52
- }
53
- }
54
- async getRepoName() {
55
- const url = await this.getGitUrl();
56
- if (url) {
57
- return `${url.owner}/${url.name}`;
58
- }
59
- }
60
- async getUser() {
61
- const config = await this.getConfig();
62
- const name2 = get(config, "user.name");
63
- const email = get(config, "user.email");
64
- return {
65
- name: name2,
66
- email
67
- };
68
- }
69
- };
70
-
71
- // src/scripts.ts
72
- var scripts = {
73
- "script:init": "monorepo init",
74
- "script:sync": "monorepo sync",
75
- "script:clean": "monorepo clean",
76
- "script:mirror": "monorepo mirror"
77
- };
78
- var scriptsEntries = Object.entries(scripts);
79
-
80
- // src/targets.ts
81
- function getAssetTargets(raw) {
82
- const list = [
83
- ".changeset",
84
- ".husky",
85
- ".vscode",
86
- ".editorconfig",
87
- ".gitattributes",
88
- ".gitignore",
89
- ".npmrc",
90
- "commitlint.config.ts",
91
- "eslint.config.js",
92
- "lint-staged.config.js",
93
- "stylelint.config.js",
94
- "package.json",
95
- // pnpm
96
- "pnpm-workspace.yaml",
97
- // base tsconfig
98
- "tsconfig.json",
99
- // turbo
100
- "turbo.json",
101
- // vitest
102
- "vitest.workspace.ts",
103
- // #region docker
104
- "Dockerfile",
105
- ".dockerignore"
106
- // #endregion
107
- ];
108
- if (!raw) {
109
- list.push(".github", "LICENSE", "renovate.json", "SECURITY.md", "CODE_OF_CONDUCT.md", "CONTRIBUTING.md", "netlify.toml");
110
- }
111
- return list;
112
- }
113
-
114
- // src/utils/md5.ts
115
- import crypto from "node:crypto";
116
- function getFileHash(data) {
117
- const hashSum = crypto.createHash("md5");
118
- hashSum.update(data);
119
- return hashSum.digest("hex");
120
- }
121
- function isFileChanged(src, dest) {
122
- try {
123
- const currentHash = getFileHash(src);
124
- const previousHash = getFileHash(dest);
125
- return currentHash !== previousHash;
126
- } catch (err) {
127
- console.error("Error calculating file hash:", err);
128
- return false;
129
- }
130
- }
131
-
132
- // src/utils/regexp.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 templatesDir = path.join(__dirname2, "../templates");
151
- var cwd = process.cwd();
152
- function isWorkspace(version2) {
153
- if (typeof version2 === "string") {
154
- return version2.startsWith("workspace:");
155
- }
156
- return false;
157
- }
158
- function setPkgJson(sourcePkgJson, targetPkgJson) {
159
- const packageManager = get2(sourcePkgJson, "packageManager", { default: "" });
160
- const deps = get2(sourcePkgJson, "dependencies", { default: {} });
161
- const devDeps = get2(sourcePkgJson, "devDependencies", { default: {} });
162
- const targetDeps = get2(targetPkgJson, "dependencies", { default: {} });
163
- const targetDevDeps = get2(targetPkgJson, "devDependencies", { default: {} });
164
- set(targetPkgJson, "packageManager", packageManager);
165
- Object.entries(deps).forEach((x) => {
166
- if (!isWorkspace(targetDeps[x[0]])) {
167
- set(targetPkgJson, `dependencies.${x[0].replaceAll(".", "\\.")}`, x[1], { preservePaths: false });
168
- }
169
- });
170
- Object.entries(devDeps).forEach((x) => {
171
- if (x[0] === name) {
172
- set(targetPkgJson, `devDependencies.${x[0].replaceAll(".", "\\.")}`, `^${version}`, { preservePaths: false });
173
- } else {
174
- if (!isWorkspace(targetDevDeps[x[0]])) {
175
- set(targetPkgJson, `devDependencies.${x[0].replaceAll(".", "\\.")}`, x[1], { preservePaths: false });
176
- }
177
- }
178
- });
179
- for (const [k, v] of scriptsEntries) {
180
- set(targetPkgJson, `scripts.${k}`, v);
181
- }
182
- }
183
- function confirmOverwrite(filename) {
184
- return confirm({ message: `${pc.greenBright(filename)} \u6587\u4EF6\u5185\u5BB9\u53D1\u751F\u6539\u53D8,\u662F\u5426\u8986\u76D6?`, default: false });
185
- }
186
- async function upgradeMonorepo(opts) {
187
- const { outDir = "", raw, interactive } = opts;
188
- const absOutDir = path.isAbsolute(outDir) ? outDir : path.join(cwd, outDir);
189
- const gitClient = new GitClient({
190
- baseDir: cwd
191
- });
192
- const repoName = await gitClient.getRepoName();
193
- let targets = getAssetTargets(raw);
194
- if (interactive) {
195
- targets = await checkbox({
196
- message: "\u9009\u62E9\u4F60\u9700\u8981\u7684\u6587\u4EF6",
197
- choices: targets.map((x) => {
198
- return {
199
- value: x,
200
- checked: true
201
- };
202
- })
203
- });
204
- }
205
- const regexpArr = targets.map((x) => {
206
- return new RegExp(`^${escapeStringRegexp(x)}`);
207
- });
208
- for await (const file of klaw(assetsDir, {
209
- filter(p) {
210
- const str = path.relative(assetsDir, p);
211
- return isMatch(str, regexpArr);
212
- }
213
- })) {
214
- await queue.add(async () => {
215
- if (file.stats.isFile()) {
216
- const relPath = path.relative(assetsDir, file.path);
217
- const targetPath = path.resolve(absOutDir, relPath);
218
- const targetIsExisted = await fs.exists(targetPath);
219
- async function overwriteOrCopy(target) {
220
- let isOverwrite = true;
221
- if (targetIsExisted) {
222
- const src = await fs.readFile(file.path);
223
- const dest = target ?? await fs.readFile(targetPath);
224
- if (await isFileChanged(src, dest)) {
225
- isOverwrite = await confirmOverwrite(relPath);
226
- }
227
- }
228
- return isOverwrite;
229
- }
230
- if (relPath === "package.json") {
231
- const sourcePath = file.path;
232
- if (targetIsExisted) {
233
- const sourcePkgJson = await fs.readJson(sourcePath);
234
- const targetPkgJson = await fs.readJson(targetPath);
235
- setPkgJson(sourcePkgJson, targetPkgJson);
236
- const data = JSON.stringify(targetPkgJson, void 0, 2);
237
- await fs.writeFile(targetPath, `${data}
238
- `, "utf8");
239
- logger.success(targetPath);
240
- }
241
- } else if (relPath === ".changeset/config.json" && repoName) {
242
- const changesetJson = await fs.readJson(file.path);
243
- set(changesetJson, "changelog.1.repo", repoName);
244
- const data = JSON.stringify(changesetJson, void 0, 2);
245
- if (await overwriteOrCopy(data)) {
246
- await fs.ensureDir(path.dirname(targetPath));
247
- await fs.writeFile(targetPath, `${data}
248
- `, "utf8");
249
- logger.success(targetPath);
250
- }
251
- } else {
252
- if (await overwriteOrCopy()) {
253
- await fs.copy(
254
- file.path,
255
- targetPath
256
- );
257
- logger.success(targetPath);
258
- }
259
- }
260
- }
261
- });
262
- }
263
- }
264
- async function createNewProject(name2) {
265
- const defaultTemplate = "bar";
266
- const targetTemplate = name2 ?? defaultTemplate;
267
- await fs.copy(path.join(templatesDir, defaultTemplate), path.join(cwd, targetTemplate));
268
- logger.success(`${targetTemplate} \u9879\u76EE\u521B\u5EFA\u6210\u529F\uFF01`);
269
- }
270
-
271
- export {
272
- name,
273
- version,
274
- logger,
275
- GitClient,
276
- setPkgJson,
277
- upgradeMonorepo,
278
- createNewProject
279
- };