jiek 0.2.0-alpha.1 → 0.2.1-alpha.1

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,395 @@
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
+ default:
317
+ throw new Error(`unsupported config file type: ${ext}`);
318
+ }
319
+ if (!module)
320
+ throw new Error("config file is empty");
321
+ return module.default ?? module;
322
+ }
323
+
324
+ const PACKAGE_JSON_TEMPLATE = `{
325
+ "name": "",
326
+ "version": "0.0.1",
327
+ "description": "",
328
+ "license": "",
329
+ "author": "",
330
+ "files": ["dist"],
331
+ "exports": {
332
+ ".": "./src/index.ts"
333
+ },
334
+ "scripts": {
335
+ },
336
+ "homepage": "",
337
+ "repository": "",
338
+ "bugs": ""
339
+ }`.trimStart();
340
+ const README_TEMPLATE = `# $name
341
+
342
+ ## Installation
343
+
344
+ \`\`\`bash
345
+ npm install $name
346
+ # or
347
+ pnpm install $name
348
+ # or
349
+ yarn add $name
350
+ \`\`\`
351
+
352
+ ## Usage
353
+
354
+
355
+ ## License
356
+
357
+ $license
358
+ `.trimStart();
359
+ function getTemplateStr(wd, template) {
360
+ let templateString = template ?? PACKAGE_JSON_TEMPLATE;
361
+ let isTemplateFile = false;
362
+ try {
363
+ if (template)
364
+ JSON.parse(template);
365
+ } catch (e) {
366
+ isTemplateFile = true;
367
+ }
368
+ if (isTemplateFile) {
369
+ const templatePath = path__default.default.resolve(wd, template);
370
+ templateString = fs__default.default.readFileSync(templatePath, "utf-8");
371
+ }
372
+ return templateString;
373
+ }
374
+ const wdCache = /* @__PURE__ */ new Map();
375
+ function getWDPackageJSONFiled(wd, field) {
376
+ if (wdCache.has(wd)) {
377
+ return wdCache.get(wd)[field];
378
+ }
379
+ const packageJSONPath = path__default.default.resolve(wd, "package.json");
380
+ const packageJSON = JSON.parse(fs__default.default.readFileSync(packageJSONPath, "utf-8"));
381
+ wdCache.set(wd, packageJSON);
382
+ return packageJSON[field];
383
+ }
384
+ async function getName(named, name, {
385
+ wd,
386
+ cwd,
387
+ workspaceName
388
+ }) {
389
+ const relativePath = cwd.replace(`${wd}/`, "");
390
+ let basename = path__default.default.basename(cwd);
391
+ if (typeof named === "function") {
392
+ return named(name, {
393
+ full: wd,
394
+ relative: cwd
395
+ });
396
+ }
397
+ let isParentMatched = false;
398
+ let matchedKey;
399
+ let matchedRule;
400
+ if (typeof named === "object") {
401
+ const isWD = cwd === wd;
402
+ if (isWD) {
403
+ const { rule } = await inquirer__default.default.prompt({
404
+ type: "list",
405
+ name: "rule",
406
+ message: "choose a rule",
407
+ default: "default",
408
+ choices: ["default"].concat(Object.keys(named))
409
+ });
410
+ if (rule !== "default") {
411
+ matchedKey = rule;
412
+ matchedRule = named[rule];
413
+ }
414
+ } else
415
+ for (const [key, value] of Object.entries(named)) {
416
+ if (micromatch.isMatch(relativePath, key)) {
417
+ matchedKey = key;
418
+ matchedRule = value;
419
+ break;
420
+ }
421
+ if (micromatch.isMatch(`${relativePath}/jiek_ignore_dont_use_same_file_name`, key)) {
422
+ isParentMatched = true;
423
+ matchedKey = key;
424
+ matchedRule = value;
425
+ break;
426
+ }
427
+ }
428
+ }
429
+ if (!matchedRule) {
430
+ matchedKey = "packages/*";
431
+ matchedRule = `@${workspaceName}/$basename`;
432
+ }
433
+ if (!matchedRule)
434
+ throw new Error("no matched rule");
435
+ if (!name && isParentMatched) {
436
+ basename = await inquirer__default.default.prompt({
437
+ type: "input",
438
+ name: "name",
439
+ message: `the matched rule is \`${String(matchedRule)}\`, please input the basename
440
+ `
441
+ }).then(({ name: name2 }) => name2);
442
+ }
443
+ if (typeof matchedRule === "function") {
444
+ return matchedRule(name, {
445
+ full: wd,
446
+ relative: cwd,
447
+ basename
448
+ });
449
+ }
450
+ if (typeof matchedRule === "string") {
451
+ const dirName = name ?? basename;
452
+ return [
453
+ matchedRule.replace(/\$basename/g, dirName),
454
+ matchedKey?.replace(/\/\*$/g, `/${dirName}`)
455
+ ];
456
+ }
457
+ throw new Error("no matched rule");
458
+ }
459
+ commander.program.command("init [name]").option("-t, --template <template>", "the package.json template file path or file content").action(async () => {
460
+ const [, name] = commander.program.args;
461
+ const cwd = process.cwd();
462
+ const { init = {} } = loadConfig() ?? {};
463
+ const { wd } = getWD();
464
+ const workspaceName = path__default.default.basename(wd);
465
+ const {
466
+ named,
467
+ template,
468
+ bug = {},
469
+ readme: _readme = README_TEMPLATE,
470
+ readmeTemplate
471
+ } = init;
472
+ const resolvedBug = {
473
+ template: "bug_report.yml",
474
+ labels: ["bug"],
475
+ ...bug
476
+ };
477
+ let readme = _readme;
478
+ if (readmeTemplate) {
479
+ const readmeTemplatePath = path__default.default.resolve(wd, readmeTemplate);
480
+ readme = fs__default.default.readFileSync(readmeTemplatePath, "utf-8");
481
+ }
482
+ const templateString = getTemplateStr(wd, template);
483
+ const { indent = " " } = detectIndent__default.default(templateString);
484
+ const formattingOptions = {
485
+ tabSize: indent.length,
486
+ insertSpaces: true
487
+ };
488
+ const passFields = [
489
+ "license",
490
+ "author"
491
+ ];
492
+ let newJSONString = templateString;
493
+ for (const field of passFields) {
494
+ newJSONString = jsoncParser.applyEdits(newJSONString, jsoncParser.modify(
495
+ newJSONString,
496
+ [field],
497
+ getWDPackageJSONFiled(wd, field),
498
+ { formattingOptions }
499
+ ));
500
+ }
501
+ let [pkgName, pkgDir] = await getName(named, name, {
502
+ wd,
503
+ cwd,
504
+ workspaceName
505
+ });
506
+ if (!pkgDir) {
507
+ const { dir } = await inquirer__default.default.prompt({
508
+ type: "input",
509
+ name: "dir",
510
+ message: "package directory",
511
+ default: name
512
+ });
513
+ pkgDir = dir;
514
+ }
515
+ if (!pkgName) {
516
+ const { name: inputName } = await inquirer__default.default.prompt({
517
+ type: "input",
518
+ name: "name",
519
+ message: "package name",
520
+ default: name
521
+ });
522
+ pkgName = inputName;
523
+ }
524
+ newJSONString = jsoncParser.applyEdits(newJSONString, jsoncParser.modify(newJSONString, ["name"], pkgName, { formattingOptions }));
525
+ let pkgRepo = getWDPackageJSONFiled(wd, "repository");
526
+ if (typeof pkgRepo === "string") {
527
+ pkgRepo = {
528
+ type: "git",
529
+ url: pkgRepo,
530
+ directory: pkgDir
531
+ };
532
+ }
533
+ newJSONString = jsoncParser.applyEdits(newJSONString, jsoncParser.modify(
534
+ newJSONString,
535
+ ["repository"],
536
+ pkgRepo,
537
+ { formattingOptions }
538
+ ));
539
+ const homepage = `${pkgRepo?.url}/blob/master/${pkgDir}/README.md`;
540
+ newJSONString = jsoncParser.applyEdits(newJSONString, jsoncParser.modify(
541
+ newJSONString,
542
+ ["homepage"],
543
+ homepage,
544
+ { formattingOptions }
545
+ ));
546
+ let labels = resolvedBug.labels;
547
+ if (typeof labels === "function")
548
+ labels = labels({
549
+ name: pkgName,
550
+ dir: pkgDir
551
+ });
552
+ labels.push(`scope:${pkgName}`);
553
+ const bugs = `${pkgRepo?.url}/issues/new?template=${resolvedBug.template}&labels=${labels.join(",")}`;
554
+ newJSONString = jsoncParser.applyEdits(newJSONString, jsoncParser.modify(
555
+ newJSONString,
556
+ ["bugs"],
557
+ bugs,
558
+ { formattingOptions }
559
+ ));
560
+ function pkgDirTo(to) {
561
+ if (!pkgDir)
562
+ throw new Error("pkgDir is not defined");
563
+ return path__default.default.resolve(pkgDir, to);
564
+ }
565
+ if (!fs__default.default.existsSync(pkgDir))
566
+ fs__default.default.mkdirSync(pkgDir);
567
+ const pkgJSONFilePath = pkgDirTo("package.json");
568
+ if (fs__default.default.existsSync(pkgJSONFilePath)) {
569
+ throw new Error("package.json already exists");
570
+ }
571
+ fs__default.default.writeFileSync(pkgJSONFilePath, newJSONString);
572
+ console.log(newJSONString, "written to", pkgJSONFilePath);
573
+ const license = getWDPackageJSONFiled(wd, "license");
574
+ const readmeFilePath = pkgDirTo("README.md");
575
+ if (typeof readme === "function") {
576
+ readme = readme({
577
+ dir: pkgDir,
578
+ packageJson: JSON.parse(newJSONString)
579
+ });
580
+ }
581
+ const readmeContent = readme.replace(/\$name/g, pkgName).replace(/\$license/g, license);
582
+ fs__default.default.writeFileSync(readmeFilePath, readmeContent);
583
+ });
584
+
179
585
  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
586
  actionRestore();
181
587
  const { value = {} } = await getSelectedProjectsGraph() ?? {};
@@ -227,7 +633,7 @@
227
633
  console.warn("preview mode");
228
634
  continue;
229
635
  }
230
- childProcess__namespace.execSync(["pnpm", "publish", "--access", "public", "--no-git-checks", ...passArgs].join(" "), {
636
+ childProcess__namespace$1.execSync(["pnpm", "publish", "--access", "public", "--no-git-checks", ...passArgs].join(" "), {
231
637
  cwd: dir,
232
638
  stdio: "inherit"
233
639
  });
@@ -241,54 +647,9 @@
241
647
  actionDone();
242
648
  });
243
649
 
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
650
  const pkg = require("../package.json");
290
- commander.program.version(pkg.version).description(pkg.description).option("--root <root>", "root path");
651
+ commander.program.version(pkg.version).description(pkg.description).option("--root <root>", "root path").option("-c, --config-path <configPath>", "config path");
291
652
  commander.program.parse(process.argv);
292
653
 
293
- })(fs, path, getWorkspaceDir, filterWorkspacePackages, commander, jsYaml, bumper, childProcess, detectIndent, jsoncParser, pkger, childProcess$1);
654
+ })(fs, path, filterWorkspacePackages, commander, jsYaml, getWorkspaceDir, childProcess, pkger, detectIndent, inquirer, jsoncParser, micromatch, bumper, childProcess$1);
294
655
  //# sourceMappingURL=index.iife.js.map