jiek 0.2.1 → 0.2.2

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,4 +1,4 @@
1
- (function (fs, path, getWorkspaceDir, filterWorkspacePackages, commander, jsYaml, bumper, childProcess, detectIndent, jsoncParser, pkger, childProcess$1) {
1
+ (function (fs, path, filterWorkspacePackages, commander, jsYaml, getWorkspaceDir, childProcess, pkger, detectIndent, inquirer, jsoncParser, micromatch, bumper, childProcess$1) {
2
2
  'use strict';
3
3
 
4
4
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
@@ -25,23 +25,24 @@
25
25
  var path__default = /*#__PURE__*/_interopDefault(path);
26
26
  var childProcess__namespace = /*#__PURE__*/_interopNamespace(childProcess);
27
27
  var detectIndent__default = /*#__PURE__*/_interopDefault(detectIndent);
28
+ var inquirer__default = /*#__PURE__*/_interopDefault(inquirer);
28
29
  var childProcess__namespace$1 = /*#__PURE__*/_interopNamespace(childProcess$1);
29
30
 
30
- let type = "";
31
- try {
32
- require.resolve("@pnpm/filter-workspace-packages");
33
- type = "pnpm";
34
- } catch {
35
- }
36
- if (type !== "") {
37
- commander.program.option("-f, --filter <filter>", "filter packages");
38
- }
39
- async function getSelectedProjectsGraph() {
40
- let filter = commander.program.getOptionValue("filter");
31
+ let root;
32
+ function getRoot() {
33
+ if (root)
34
+ return root;
41
35
  const rootOption = commander.program.getOptionValue("root");
42
- const root = rootOption ? path__default.default.isAbsolute(rootOption) ? rootOption : path__default.default.resolve(process.cwd(), rootOption) : process.cwd();
43
- let notWorkspace = false;
44
- let wd;
36
+ root = rootOption ? path__default.default.isAbsolute(rootOption) ? rootOption : path__default.default.resolve(process.cwd(), rootOption) : process.cwd();
37
+ return root;
38
+ }
39
+
40
+ let wd;
41
+ let notWorkspace = false;
42
+ function getWD() {
43
+ if (wd)
44
+ return { wd, notWorkspace };
45
+ const root = getRoot();
45
46
  try {
46
47
  wd = getWorkspaceDir.getWorkspaceDir(root, type);
47
48
  } catch (e) {
@@ -52,6 +53,22 @@
52
53
  throw e;
53
54
  }
54
55
  }
56
+ return { wd, notWorkspace };
57
+ }
58
+
59
+ let type = "";
60
+ try {
61
+ require.resolve("@pnpm/filter-workspace-packages");
62
+ type = "pnpm";
63
+ } catch {
64
+ }
65
+ if (type !== "") {
66
+ commander.program.option("-f, --filter <filter>", "filter packages");
67
+ }
68
+ async function getSelectedProjectsGraph() {
69
+ let filter = commander.program.getOptionValue("filter");
70
+ const root = getRoot();
71
+ const { wd, notWorkspace } = getWD();
55
72
  if (!notWorkspace && type === "pnpm") {
56
73
  const pnpmWorkspaceFilePath = path__default.default.resolve(wd, "pnpm-workspace.yaml");
57
74
  const pnpmWorkspaceFileContent = fs__default.default.readFileSync(pnpmWorkspaceFilePath, "utf-8");
@@ -176,6 +193,398 @@
176
193
  };
177
194
  }
178
195
 
196
+ const FILE_TEMPLATE = (manifest) => `
197
+ const pkg = ${JSON.stringify(manifest, null, 2)}
198
+ const { jiek = {} } = pkg
199
+ const templateArg = jiek.templateArgFilePath
200
+ ? require.resolve(jiek.templateArgFilePath)
201
+ : {
202
+ styled: jiek.styled
203
+ }
204
+ module.exports = require('jiek/rollup').template(templateArg, pkg)
205
+ `.trimStart();
206
+ commander.program.command("build").action(async () => {
207
+ actionRestore();
208
+ const {
209
+ wd,
210
+ value = {}
211
+ } = await getSelectedProjectsGraph() ?? {};
212
+ if (Object.keys(value).length === 0) {
213
+ throw new Error("no package found");
214
+ }
215
+ const jiekTempDir = (...paths) => path__default.default.resolve(wd, "node_modules/.jiek", ...paths);
216
+ if (!fs__default.default.existsSync(jiekTempDir()))
217
+ fs__default.default.mkdirSync(jiekTempDir());
218
+ const rollupBinaryPath = require.resolve("rollup").replace(/dist\/rollup.js$/, "dist/bin/rollup");
219
+ let i = 0;
220
+ for (const [dir, manifest] of Object.entries(value)) {
221
+ const newManifest = mergePackageJson(manifest, dir);
222
+ const escapeManifestName = manifest.name?.replace(/^@/g, "").replace(/\//g, "+");
223
+ const configFile = jiekTempDir(
224
+ `${escapeManifestName ?? `anonymous-${i++}`}.rollup.config.js`
225
+ );
226
+ fs__default.default.writeFileSync(configFile, FILE_TEMPLATE(newManifest));
227
+ let prefix = "";
228
+ if (process.env.NODE_ENV === "test") {
229
+ const registerPath = require.resolve("esbuild-register");
230
+ const loaderPath = require.resolve("esbuild-register/loader");
231
+ prefix = `node --import ${registerPath} -r ${loaderPath} `;
232
+ }
233
+ childProcess__namespace.execSync(`${prefix}${rollupBinaryPath} -c ${configFile}`, {
234
+ cwd: dir,
235
+ stdio: "inherit"
236
+ });
237
+ }
238
+ actionDone();
239
+ });
240
+
241
+ function packageIsExist(name) {
242
+ try {
243
+ require.resolve(name);
244
+ return true;
245
+ } catch (e) {
246
+ console.log(e);
247
+ return false;
248
+ }
249
+ }
250
+ let tsRegisterName;
251
+ const registers = [
252
+ process.env.JIEK_TS_REGISTER,
253
+ "esbuild-register"
254
+ ].filter(Boolean);
255
+ for (const register of registers) {
256
+ if (packageIsExist(register)) {
257
+ tsRegisterName = register;
258
+ break;
259
+ }
260
+ }
261
+ let configName = "jiek.config";
262
+ function getConfigPath(root) {
263
+ const isSupportTsLoader = !!tsRegisterName;
264
+ function configWithExtIsExist(ext) {
265
+ const filenames = [
266
+ path__default.default.resolve(root, `${configName}.${ext}`),
267
+ path__default.default.resolve(root, `.${configName}.${ext}`)
268
+ ];
269
+ for (const filename of filenames) {
270
+ if (fs__default.default.existsSync(filename) && fs__default.default.lstatSync(filename).isFile()) {
271
+ return filename;
272
+ }
273
+ }
274
+ return;
275
+ }
276
+ configName = configWithExtIsExist("js") ?? configName;
277
+ configName = configWithExtIsExist("json") ?? configName;
278
+ configName = configWithExtIsExist("yaml") ?? configName;
279
+ if (isSupportTsLoader) {
280
+ configName = configWithExtIsExist("ts") ?? configName;
281
+ }
282
+ return path__default.default.resolve(root, configName);
283
+ }
284
+ function loadConfig() {
285
+ const { wd: root, notWorkspace } = getWD();
286
+ if (notWorkspace)
287
+ throw new Error("not in workspace");
288
+ let configPath = commander.program.getOptionValue("configPath");
289
+ if (!configPath) {
290
+ configPath = getConfigPath(root);
291
+ } else {
292
+ if (!fs__default.default.existsSync(configPath))
293
+ throw new Error(`config file not found: ${configPath}`);
294
+ if (!path__default.default.isAbsolute(configPath))
295
+ configPath = path__default.default.resolve(root, configPath);
296
+ }
297
+ const ext = path__default.default.extname(configPath);
298
+ let module;
299
+ switch (ext) {
300
+ case ".js":
301
+ module = require(configPath);
302
+ break;
303
+ case ".json":
304
+ return require(configPath);
305
+ case ".yaml":
306
+ return jsYaml.load(fs__default.default.readFileSync(configPath, "utf-8"));
307
+ case ".ts":
308
+ if (tsRegisterName) {
309
+ require(tsRegisterName);
310
+ module = require(configPath);
311
+ break;
312
+ }
313
+ throw new Error(
314
+ "ts config file is not supported without ts register, please install esbuild-register or set JIEK_TS_REGISTER env for custom ts register"
315
+ );
316
+ case ".config":
317
+ module = {};
318
+ break;
319
+ default:
320
+ throw new Error(`unsupported config file type: ${ext}`);
321
+ }
322
+ if (!module)
323
+ throw new Error("config file is empty");
324
+ return module.default ?? module;
325
+ }
326
+
327
+ const PACKAGE_JSON_TEMPLATE = `{
328
+ "name": "",
329
+ "version": "0.0.1",
330
+ "description": "",
331
+ "license": "",
332
+ "author": "",
333
+ "files": ["dist"],
334
+ "exports": {
335
+ ".": "./src/index.ts"
336
+ },
337
+ "scripts": {
338
+ },
339
+ "homepage": "",
340
+ "repository": "",
341
+ "bugs": ""
342
+ }`.trimStart();
343
+ const README_TEMPLATE = `# $name
344
+
345
+ ## Installation
346
+
347
+ \`\`\`bash
348
+ npm install $name
349
+ # or
350
+ pnpm install $name
351
+ # or
352
+ yarn add $name
353
+ \`\`\`
354
+
355
+ ## Usage
356
+
357
+
358
+ ## License
359
+
360
+ $license
361
+ `.trimStart();
362
+ function getTemplateStr(wd, template) {
363
+ let templateString = template ?? PACKAGE_JSON_TEMPLATE;
364
+ let isTemplateFile = false;
365
+ try {
366
+ if (template)
367
+ JSON.parse(template);
368
+ } catch (e) {
369
+ isTemplateFile = true;
370
+ }
371
+ if (isTemplateFile) {
372
+ const templatePath = path__default.default.resolve(wd, template);
373
+ templateString = fs__default.default.readFileSync(templatePath, "utf-8");
374
+ }
375
+ return templateString;
376
+ }
377
+ const wdCache = /* @__PURE__ */ new Map();
378
+ function getWDPackageJSONFiled(wd, field) {
379
+ if (wdCache.has(wd)) {
380
+ return wdCache.get(wd)[field];
381
+ }
382
+ const packageJSONPath = path__default.default.resolve(wd, "package.json");
383
+ const packageJSON = JSON.parse(fs__default.default.readFileSync(packageJSONPath, "utf-8"));
384
+ wdCache.set(wd, packageJSON);
385
+ return packageJSON[field];
386
+ }
387
+ async function getName(named, name, {
388
+ wd,
389
+ cwd,
390
+ workspaceName
391
+ }) {
392
+ const relativePath = cwd.replace(`${wd}/`, "");
393
+ let basename = path__default.default.basename(cwd);
394
+ if (typeof named === "function") {
395
+ return named(name, {
396
+ full: wd,
397
+ relative: cwd
398
+ });
399
+ }
400
+ let isParentMatched = false;
401
+ let matchedKey;
402
+ let matchedRule;
403
+ if (typeof named === "object") {
404
+ const isWD = cwd === wd;
405
+ if (isWD) {
406
+ const { rule } = await inquirer__default.default.prompt({
407
+ type: "list",
408
+ name: "rule",
409
+ message: "choose a rule",
410
+ default: "default",
411
+ choices: ["default"].concat(Object.keys(named))
412
+ });
413
+ if (rule !== "default") {
414
+ matchedKey = rule;
415
+ matchedRule = named[rule];
416
+ }
417
+ } else
418
+ for (const [key, value] of Object.entries(named)) {
419
+ if (micromatch.isMatch(relativePath, key)) {
420
+ matchedKey = key;
421
+ matchedRule = value;
422
+ break;
423
+ }
424
+ if (micromatch.isMatch(`${relativePath}/jiek_ignore_dont_use_same_file_name`, key)) {
425
+ isParentMatched = true;
426
+ matchedKey = key;
427
+ matchedRule = value;
428
+ break;
429
+ }
430
+ }
431
+ }
432
+ if (!matchedRule) {
433
+ matchedKey = "packages/*";
434
+ matchedRule = `@${workspaceName}/$basename`;
435
+ }
436
+ if (!matchedRule)
437
+ throw new Error("no matched rule");
438
+ if (!name && isParentMatched) {
439
+ basename = await inquirer__default.default.prompt({
440
+ type: "input",
441
+ name: "name",
442
+ message: `the matched rule is \`${String(matchedRule)}\`, please input the basename
443
+ `
444
+ }).then(({ name: name2 }) => name2);
445
+ }
446
+ if (typeof matchedRule === "function") {
447
+ return matchedRule(name, {
448
+ full: wd,
449
+ relative: cwd,
450
+ basename
451
+ });
452
+ }
453
+ if (typeof matchedRule === "string") {
454
+ const dirName = name ?? basename;
455
+ return [
456
+ matchedRule.replace(/\$basename/g, dirName),
457
+ matchedKey?.replace(/\/\*$/g, `/${dirName}`)
458
+ ];
459
+ }
460
+ throw new Error("no matched rule");
461
+ }
462
+ commander.program.command("init [name]").option("-t, --template <template>", "the package.json template file path or file content").action(async () => {
463
+ const [, name] = commander.program.args;
464
+ const cwd = process.cwd();
465
+ const { init = {} } = loadConfig() ?? {};
466
+ const { wd } = getWD();
467
+ const workspaceName = path__default.default.basename(wd);
468
+ const {
469
+ named,
470
+ template,
471
+ bug = {},
472
+ readme: _readme = README_TEMPLATE,
473
+ readmeTemplate
474
+ } = init;
475
+ const resolvedBug = {
476
+ template: "bug_report.yml",
477
+ labels: ["bug"],
478
+ ...bug
479
+ };
480
+ let readme = _readme;
481
+ if (readmeTemplate) {
482
+ const readmeTemplatePath = path__default.default.resolve(wd, readmeTemplate);
483
+ readme = fs__default.default.readFileSync(readmeTemplatePath, "utf-8");
484
+ }
485
+ const templateString = getTemplateStr(wd, template);
486
+ const { indent = " " } = detectIndent__default.default(templateString);
487
+ const formattingOptions = {
488
+ tabSize: indent.length,
489
+ insertSpaces: true
490
+ };
491
+ const passFields = [
492
+ "license",
493
+ "author"
494
+ ];
495
+ let newJSONString = templateString;
496
+ for (const field of passFields) {
497
+ newJSONString = jsoncParser.applyEdits(newJSONString, jsoncParser.modify(
498
+ newJSONString,
499
+ [field],
500
+ getWDPackageJSONFiled(wd, field),
501
+ { formattingOptions }
502
+ ));
503
+ }
504
+ let [pkgName, pkgDir] = await getName(named, name, {
505
+ wd,
506
+ cwd,
507
+ workspaceName
508
+ });
509
+ if (!pkgDir) {
510
+ const { dir } = await inquirer__default.default.prompt({
511
+ type: "input",
512
+ name: "dir",
513
+ message: "package directory",
514
+ default: name
515
+ });
516
+ pkgDir = dir;
517
+ }
518
+ if (!pkgName) {
519
+ const { name: inputName } = await inquirer__default.default.prompt({
520
+ type: "input",
521
+ name: "name",
522
+ message: "package name",
523
+ default: name
524
+ });
525
+ pkgName = inputName;
526
+ }
527
+ newJSONString = jsoncParser.applyEdits(newJSONString, jsoncParser.modify(newJSONString, ["name"], pkgName, { formattingOptions }));
528
+ let pkgRepo = getWDPackageJSONFiled(wd, "repository");
529
+ if (typeof pkgRepo === "string") {
530
+ pkgRepo = {
531
+ type: "git",
532
+ url: pkgRepo,
533
+ directory: pkgDir
534
+ };
535
+ }
536
+ newJSONString = jsoncParser.applyEdits(newJSONString, jsoncParser.modify(
537
+ newJSONString,
538
+ ["repository"],
539
+ pkgRepo,
540
+ { formattingOptions }
541
+ ));
542
+ const homepage = `${pkgRepo?.url}/blob/master/${pkgDir}/README.md`;
543
+ newJSONString = jsoncParser.applyEdits(newJSONString, jsoncParser.modify(
544
+ newJSONString,
545
+ ["homepage"],
546
+ homepage,
547
+ { formattingOptions }
548
+ ));
549
+ let labels = resolvedBug.labels;
550
+ if (typeof labels === "function")
551
+ labels = labels({
552
+ name: pkgName,
553
+ dir: pkgDir
554
+ });
555
+ labels.push(`scope:${pkgName}`);
556
+ const bugs = `${pkgRepo?.url}/issues/new?template=${resolvedBug.template}&labels=${labels.join(",")}`;
557
+ newJSONString = jsoncParser.applyEdits(newJSONString, jsoncParser.modify(
558
+ newJSONString,
559
+ ["bugs"],
560
+ bugs,
561
+ { formattingOptions }
562
+ ));
563
+ function pkgDirTo(to) {
564
+ if (!pkgDir)
565
+ throw new Error("pkgDir is not defined");
566
+ return path__default.default.resolve(pkgDir, to);
567
+ }
568
+ if (!fs__default.default.existsSync(pkgDir))
569
+ fs__default.default.mkdirSync(pkgDir);
570
+ const pkgJSONFilePath = pkgDirTo("package.json");
571
+ if (fs__default.default.existsSync(pkgJSONFilePath)) {
572
+ throw new Error("package.json already exists");
573
+ }
574
+ fs__default.default.writeFileSync(pkgJSONFilePath, newJSONString);
575
+ console.log(newJSONString, "written to", pkgJSONFilePath);
576
+ const license = getWDPackageJSONFiled(wd, "license");
577
+ const readmeFilePath = pkgDirTo("README.md");
578
+ if (typeof readme === "function") {
579
+ readme = readme({
580
+ dir: pkgDir,
581
+ packageJson: JSON.parse(newJSONString)
582
+ });
583
+ }
584
+ const readmeContent = readme.replace(/\$name/g, pkgName).replace(/\$license/g, license);
585
+ fs__default.default.writeFileSync(readmeFilePath, readmeContent);
586
+ });
587
+
179
588
  commander.program.command("publish").aliases(["pub", "p"]).option("-b, --bumper <bumper>", "bump version", "patch").option("-p, --preview", "preview publish").action(async ({ preview, bumper: bumper$1, ...options }) => {
180
589
  actionRestore();
181
590
  const { value = {} } = await getSelectedProjectsGraph() ?? {};
@@ -227,7 +636,7 @@
227
636
  console.warn("preview mode");
228
637
  continue;
229
638
  }
230
- childProcess__namespace.execSync(["pnpm", "publish", "--access", "public", "--no-git-checks", ...passArgs].join(" "), {
639
+ childProcess__namespace$1.execSync(["pnpm", "publish", "--access", "public", "--no-git-checks", ...passArgs].join(" "), {
231
640
  cwd: dir,
232
641
  stdio: "inherit"
233
642
  });
@@ -241,54 +650,9 @@
241
650
  actionDone();
242
651
  });
243
652
 
244
- const FILE_TEMPLATE = (manifest) => `
245
- const pkg = ${JSON.stringify(manifest, null, 2)}
246
- const { jiek = {} } = pkg
247
- const templateArg = jiek.templateArgFilePath
248
- ? require.resolve(jiek.templateArgFilePath)
249
- : {
250
- styled: jiek.styled
251
- }
252
- module.exports = require('jiek/rollup').template(templateArg, pkg)
253
- `.trimStart();
254
- commander.program.command("build").action(async () => {
255
- actionRestore();
256
- const {
257
- wd,
258
- value = {}
259
- } = await getSelectedProjectsGraph() ?? {};
260
- if (Object.keys(value).length === 0) {
261
- throw new Error("no package found");
262
- }
263
- const jiekTempDir = (...paths) => path__default.default.resolve(wd, "node_modules/.jiek", ...paths);
264
- if (!fs__default.default.existsSync(jiekTempDir()))
265
- fs__default.default.mkdirSync(jiekTempDir());
266
- const rollupBinaryPath = require.resolve("rollup").replace(/dist\/rollup.js$/, "dist/bin/rollup");
267
- let i = 0;
268
- for (const [dir, manifest] of Object.entries(value)) {
269
- const newManifest = mergePackageJson(manifest, dir);
270
- const escapeManifestName = manifest.name?.replace(/^@/g, "").replace(/\//g, "+");
271
- const configFile = jiekTempDir(
272
- `${escapeManifestName ?? `anonymous-${i++}`}.rollup.config.js`
273
- );
274
- fs__default.default.writeFileSync(configFile, FILE_TEMPLATE(newManifest));
275
- let prefix = "";
276
- if (process.env.NODE_ENV === "test") {
277
- const registerPath = require.resolve("esbuild-register");
278
- const loaderPath = require.resolve("esbuild-register/loader");
279
- prefix = `node --import ${registerPath} -r ${loaderPath} `;
280
- }
281
- childProcess__namespace$1.execSync(`${prefix}${rollupBinaryPath} -c ${configFile}`, {
282
- cwd: dir,
283
- stdio: "inherit"
284
- });
285
- }
286
- actionDone();
287
- });
288
-
289
653
  const pkg = require("../package.json");
290
- commander.program.version(pkg.version).description(pkg.description).option("--root <root>", "root path");
654
+ commander.program.version(pkg.version).description(pkg.description).option("--root <root>", "root path").option("-c, --config-path <configPath>", "config path");
291
655
  commander.program.parse(process.argv);
292
656
 
293
- })(fs, path, getWorkspaceDir, filterWorkspacePackages, commander, jsYaml, bumper, childProcess, detectIndent, jsoncParser, pkger, childProcess$1);
657
+ })(fs, path, filterWorkspacePackages, commander, jsYaml, getWorkspaceDir, childProcess, pkger, detectIndent, inquirer, jsoncParser, micromatch, bumper, childProcess$1);
294
658
  //# sourceMappingURL=index.iife.js.map