@emulsify/cli 2.4.2 → 2.5.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.
Files changed (39) hide show
  1. package/README.md +10 -3
  2. package/dist/handlers/cacheClear.js +5 -6
  3. package/dist/handlers/componentCreate.js +8 -8
  4. package/dist/handlers/componentEjectTemplates.js +22 -20
  5. package/dist/handlers/componentInstall.js +101 -73
  6. package/dist/handlers/componentList.js +51 -4
  7. package/dist/handlers/hofs/withEmulsifySystem.js +3 -1
  8. package/dist/handlers/hofs/withSession.js +8 -0
  9. package/dist/handlers/init.js +144 -132
  10. package/dist/handlers/systemCreate.js +31 -25
  11. package/dist/handlers/systemDetach.js +5 -5
  12. package/dist/handlers/systemInstall.js +62 -55
  13. package/dist/handlers/systemList.js +13 -2
  14. package/dist/index.js +57 -34
  15. package/dist/lib/log.js +21 -24
  16. package/dist/lib/rootHelp.js +30 -5
  17. package/dist/lib/session.js +141 -0
  18. package/dist/schemas/emulsifyProjectConfig.json +11 -0
  19. package/dist/schemas/system.json +1 -0
  20. package/dist/schemas/variant.json +8 -0
  21. package/dist/util/cache/cloneIntoCache.js +25 -8
  22. package/dist/util/cache/copyItemFromCache.js +7 -1
  23. package/dist/util/cache/getCachedItemPath.js +3 -1
  24. package/dist/util/fs/writeToJsonFile.js +4 -1
  25. package/dist/util/getGitRepoNameFromUrl.js +21 -10
  26. package/dist/util/getNonInteractiveGitEnvironment.js +1 -1
  27. package/dist/util/platform/getInitSuccessMessageForPlatform.js +15 -9
  28. package/dist/util/project/componentNpmDependencies.js +169 -0
  29. package/dist/util/project/componentNpmManifest.js +164 -0
  30. package/dist/util/project/generateComponent.js +98 -39
  31. package/dist/util/project/installComponentNpmDependencies.js +197 -0
  32. package/dist/util/project/installDependencies.js +10 -3
  33. package/dist/util/project/installGeneralAssetsFromCache.js +2 -1
  34. package/dist/util/project/resolveComponentTemplate.js +11 -1
  35. package/dist/util/project/rewriteComponentIds.js +69 -0
  36. package/dist/util/prompt/index.js +21 -6
  37. package/dist/util/system/validateSystemConfig.js +13 -27
  38. package/package.json +16 -18
  39. package/dist/handlers/hofs/withProgressBar.js +0 -10
@@ -0,0 +1,69 @@
1
+ import { isUtf8 } from 'node:buffer';
2
+ import { promises as fs } from 'fs';
3
+ import { extname, join } from 'path';
4
+ import log from '../../lib/log.js';
5
+ const twigReferenceExtensions = new Set([
6
+ '.twig',
7
+ '.js',
8
+ '.mjs',
9
+ '.md',
10
+ '.mdx',
11
+ ]);
12
+ export default async function rewriteComponentIds(path, systemName, machineName, { dryRun = false } = {}) {
13
+ const report = { count: 0, files: [] };
14
+ if (systemName === machineName)
15
+ return report;
16
+ const id = new RegExp(`(['"])${RegExp.escape(systemName)}:([\\w-]+)\\1`, 'g');
17
+ async function visit(file) {
18
+ const stat = await fs.lstat(file).catch((error) => {
19
+ if (dryRun && error.code === 'ENOENT')
20
+ return undefined;
21
+ throw error;
22
+ });
23
+ if (!stat)
24
+ return;
25
+ if (stat.isDirectory()) {
26
+ for (const entry of (await fs.readdir(file)).sort()) {
27
+ await visit(join(file, entry));
28
+ }
29
+ return;
30
+ }
31
+ if (!stat.isFile() || !twigReferenceExtensions.has(extname(file)))
32
+ return;
33
+ const bytes = await fs.readFile(file);
34
+ if (!isUtf8(bytes))
35
+ return;
36
+ const original = bytes.toString('utf8');
37
+ if (/[\x00-\x08\x0e-\x1f]/.test(original))
38
+ return;
39
+ let count = 0;
40
+ const rewritten = original.replace(id, (_match, quote, component) => {
41
+ count++;
42
+ return `${quote}${machineName}:${component}${quote}`;
43
+ });
44
+ if (count === 0)
45
+ return;
46
+ if (!dryRun) {
47
+ const mode = stat.mode & 0o7777;
48
+ const readOnly = (mode & 0o200) === 0;
49
+ if (readOnly)
50
+ await fs.chmod(file, mode | 0o200);
51
+ try {
52
+ await fs.writeFile(file, rewritten);
53
+ }
54
+ finally {
55
+ if (readOnly)
56
+ await fs.chmod(file, mode);
57
+ }
58
+ }
59
+ report.count += count;
60
+ report.files.push(file);
61
+ }
62
+ await visit(path);
63
+ return report;
64
+ }
65
+ export function logComponentIdRewrites(count) {
66
+ if (count > 0) {
67
+ log('info', `Rewrote ${count} component ID reference${count === 1 ? '' : 's'}.`);
68
+ }
69
+ }
@@ -1,14 +1,29 @@
1
+ import { isCancel } from '@clack/prompts';
1
2
  import CliError from '../../lib/CliError.js';
2
- export function isInteractiveTerminal() {
3
- return process.stdin.isTTY === true;
3
+ export const PROMPT_CANCELLED_EXIT_CODE = 130;
4
+ export const PROMPT_CANCELLED_MESSAGE = 'Cancelled. Nothing was changed.';
5
+ export class PromptCancelledError extends CliError {
6
+ constructor(message = PROMPT_CANCELLED_MESSAGE) {
7
+ super(message, PROMPT_CANCELLED_EXIT_CODE);
8
+ this.name = 'PromptCancelledError';
9
+ }
10
+ }
11
+ export function isInteractiveTerminal(input = process.stdin, environment = process.env) {
12
+ return input.isTTY === true && !environment.CI;
4
13
  }
5
14
  export function requireInteractiveTerminal(nonInteractiveError) {
6
15
  if (!isInteractiveTerminal()) {
7
16
  throw new CliError(nonInteractiveError);
8
17
  }
9
18
  }
10
- export function isExitPromptError(error) {
11
- return error instanceof Error && error.name === 'ExitPromptError';
19
+ export function isPromptCancelledError(error) {
20
+ return error instanceof PromptCancelledError;
21
+ }
22
+ export function unwrapPromptResult(result) {
23
+ if (isCancel(result)) {
24
+ throw new PromptCancelledError();
25
+ }
26
+ return result;
12
27
  }
13
28
  export async function runPrompt({ prompt, nonInteractive, accept, }) {
14
29
  if (accept?.when === true) {
@@ -16,10 +31,10 @@ export async function runPrompt({ prompt, nonInteractive, accept, }) {
16
31
  }
17
32
  if ('error' in nonInteractive) {
18
33
  requireInteractiveTerminal(nonInteractive.error);
19
- return prompt();
34
+ return unwrapPromptResult(await prompt());
20
35
  }
21
36
  if (!isInteractiveTerminal()) {
22
37
  return nonInteractive.value;
23
38
  }
24
- return prompt();
39
+ return unwrapPromptResult(await prompt());
25
40
  }
@@ -1,45 +1,27 @@
1
1
  import { Ajv } from 'ajv';
2
2
  import addFormats from 'ajv-formats';
3
+ import { lt, valid } from 'semver';
3
4
  import CliError from '../../lib/CliError.js';
4
5
  import log from '../../lib/log.js';
5
6
  const upgradeCommand = 'npm i -g @emulsify/cli@latest';
6
- async function loadSchemas() {
7
- const [{ default: systemSchema }, { default: variantSchema }] = await Promise.all([
7
+ async function createValidator() {
8
+ const [{ default: systemSchema }, { default: variantSchema }, { default: packageInfo },] = await Promise.all([
8
9
  import('../../schemas/system.json', { with: { type: 'json' } }),
9
10
  import('../../schemas/variant.json', { with: { type: 'json' } }),
10
- ]);
11
- return { systemSchema, variantSchema };
12
- }
13
- async function createValidator() {
14
- const [schemas, { default: packageInfo }] = await Promise.all([
15
- loadSchemas(),
16
11
  import('../../../package.json', { with: { type: 'json' } }),
17
12
  ]);
18
13
  const ajv = new Ajv();
19
14
  addFormats(ajv, ['uri']);
20
- ajv.addSchema(schemas.variantSchema, 'variant.json');
15
+ ajv.addFormat('semver', (value) => valid(value) !== null);
16
+ ajv.addSchema(variantSchema, 'variant.json');
21
17
  return {
22
- validate: ajv.compile(schemas.systemSchema),
23
- schemas,
18
+ validate: ajv.compile(systemSchema),
19
+ schemas: { systemSchema, variantSchema },
24
20
  runningVersion: packageInfo.version,
25
21
  };
26
22
  }
27
23
  let validatorPromise;
28
- function checkMinimumCliVersion({ name, minimumCliVersion }, runningVersion) {
29
- if (minimumCliVersion === undefined)
30
- return;
31
- const minimum = minimumCliVersion.match(/^(\d+)\.(\d+)\.(\d+)(?:[-+].+)?$/);
32
- if (!minimum) {
33
- throw new CliError(`The ${JSON.stringify(name)} system has an invalid minimumCliVersion ${JSON.stringify(minimumCliVersion)}. Use a version such as "2.5.0".`);
34
- }
35
- const running = runningVersion.split(/[.+-]/, 3).map(Number);
36
- const required = minimum.slice(1).map(Number);
37
- const index = required.findIndex((part, i) => part !== running[i]);
38
- if (index !== -1 && running[index] < required[index]) {
39
- throw new CliError(`The ${JSON.stringify(name)} system requires Emulsify CLI ${minimumCliVersion} or newer. You are running ${runningVersion}. Upgrade with: ${upgradeCommand}`);
40
- }
41
- }
42
- function warnUnknownProperties(systemConfig, { systemSchema, variantSchema }) {
24
+ function warnUnknownProperties(systemConfig, { systemSchema, variantSchema, }) {
43
25
  const unknown = [];
44
26
  const collect = (value, properties, location) => {
45
27
  const keys = Object.keys(value).filter((key) => !Object.hasOwn(properties, key));
@@ -72,7 +54,11 @@ export default async function validateSystemConfig(systemConfig) {
72
54
  const { validate, schemas, runningVersion } = await (validatorPromise ??=
73
55
  createValidator());
74
56
  if (validate(systemConfig)) {
75
- checkMinimumCliVersion(systemConfig, runningVersion);
57
+ const { name, minimumCliVersion } = systemConfig;
58
+ if (minimumCliVersion !== undefined &&
59
+ lt(runningVersion.split(/[+-]/, 1)[0], minimumCliVersion.split(/[+-]/, 1)[0])) {
60
+ throw new CliError(`The ${JSON.stringify(name)} system requires Emulsify CLI ${minimumCliVersion} or newer. You are running ${runningVersion}. Upgrade with: ${upgradeCommand}`);
61
+ }
76
62
  warnUnknownProperties(systemConfig, schemas);
77
63
  return { valid: true, systemConfig };
78
64
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@emulsify/cli",
3
3
  "productName": "Emulsify CLI",
4
- "version": "2.4.2",
4
+ "version": "2.5.1",
5
5
  "description": "Build and use component systems in Drupal, WordPress, or standalone front ends.",
6
6
  "repository": "git@github.com:emulsify-ds/emulsify-cli.git",
7
7
  "author": "Patrick Coffey <patrickcoffey48@gmail.com>",
@@ -49,37 +49,35 @@
49
49
  "prepare": "husky"
50
50
  },
51
51
  "devDependencies": {
52
- "@commitlint/cli": "^21.2.2",
53
- "@commitlint/config-conventional": "^21.2.2",
52
+ "@commitlint/cli": "^21.2.4",
53
+ "@commitlint/config-conventional": "^21.2.4",
54
54
  "@semantic-release/commit-analyzer": "^13.0.1",
55
- "@semantic-release/github": "^12.0.9",
56
- "@semantic-release/npm": "^13.1.5",
55
+ "@semantic-release/github": "^12.0.11",
56
+ "@semantic-release/npm": "^13.2.0",
57
57
  "@semantic-release/release-notes-generator": "^14.1.1",
58
58
  "@types/fs-extra": "^11.0.4",
59
59
  "@types/jest": "^30.0.0",
60
- "@types/node": "^26.4.0",
61
- "@types/progress": "^2.0.7",
60
+ "@types/node": "^26.6.10",
61
+ "@types/semver": "^7.8.0",
62
62
  "husky": "^9.1.7",
63
- "jest": "^30.5.0",
63
+ "jest": "^30.5.2",
64
64
  "json-schema-to-typescript": "^16.0.0",
65
- "lint-staged": "^17.4.1",
65
+ "lint-staged": "^17.6.2",
66
66
  "nodemon": "^3.1.14",
67
- "prettier": "^3.9.6",
67
+ "prettier": "^3.10.1",
68
68
  "semantic-release": "^25.0.9",
69
- "ts-jest": "^29.4.12",
69
+ "ts-jest": "^29.4.15",
70
70
  "ts-node": "^10.9.2",
71
71
  "typescript": "^6.0.3"
72
72
  },
73
73
  "dependencies": {
74
- "@inquirer/prompts": "^8.7.0",
74
+ "@clack/prompts": "^1.9.0",
75
75
  "ajv": "^8.20.0",
76
76
  "ajv-formats": "^3.0.1",
77
- "boxen": "^8.0.1",
78
77
  "colorette": "^2.0.20",
79
- "commander": "^15.0.0",
80
- "consola": "^3.4.2",
81
- "fs-extra": "^11.4.0",
82
- "progress": "^2.0.3",
83
- "simple-git": "^4.0.2"
78
+ "commander": "^15.1.0",
79
+ "fs-extra": "^11.4.1",
80
+ "semver": "^7.8.6",
81
+ "simple-git": "^4.1.0"
84
82
  }
85
83
  }
@@ -1,10 +0,0 @@
1
- import ProgressBar from 'progress';
2
- export default function withProgressBar(handler) {
3
- const progress = new ProgressBar('[:bar] :percent :message', {
4
- total: 100,
5
- complete: '=',
6
- incomplete: ' ',
7
- width: 100,
8
- });
9
- return handler(progress);
10
- }