@emulsify/cli 2.5.0 → 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.
@@ -1,4 +1,5 @@
1
1
  import { pathExists } from 'fs-extra';
2
+ import { dirname, join, relative } from 'path';
2
3
  import { confirm, select } from '@clack/prompts';
3
4
  import log from '../lib/log.js';
4
5
  import { markSessionMayHaveChanges, note } from '../lib/session.js';
@@ -7,10 +8,12 @@ import CliError from '../lib/CliError.js';
7
8
  import installComponentFromCache, { getComponentDestination, } from '../util/project/installComponentFromCache.js';
8
9
  import buildComponentDependencyList from '../util/project/buildComponentDependencyList.js';
9
10
  import { applyComponentNpmInstall, confirmComponentNpmInstall, formatAddedNpmPackages, formatComponentNpmInstall, prepareComponentNpmInstall, } from '../util/project/installComponentNpmDependencies.js';
11
+ import rewriteComponentIds, { logComponentIdRewrites, } from '../util/project/rewriteComponentIds.js';
12
+ import getCachedItemPath from '../util/cache/getCachedItemPath.js';
10
13
  import catchLater from '../util/catchLater.js';
11
14
  import findFileInCurrentPath from '../util/fs/findFileInCurrentPath.js';
12
15
  import { requireInteractiveTerminal, runPrompt } from '../util/prompt/index.js';
13
- import { withEmulsifySystem } from './hofs/withEmulsifySystem.js';
16
+ import { withEmulsifySystem, } from './hofs/withEmulsifySystem.js';
14
17
  const INSTALL_ALL_COMPONENTS = Symbol('install all components');
15
18
  const MISSING_COMPONENT_INSTALL_TARGET_ERROR = 'Please specify a component to install, or pass --all to install all available components.';
16
19
  function getDryRunInstallAction(exists, force) {
@@ -22,20 +25,31 @@ function getDryRunInstallAction(exists, force) {
22
25
  }
23
26
  return 'prompt before replacing or skipping';
24
27
  }
25
- async function buildComponentInstallPlan(variant, componentNames, rootComponentName, force, projectConfigPath) {
28
+ async function buildComponentInstallPlan({ emulsifyConfig, systemConf, variantConf }, componentNames, rootComponentName, force, projectConfigPath) {
26
29
  if (!projectConfigPath) {
27
30
  throw new CliError('Unable to find an Emulsify project to preview component installation into.');
28
31
  }
29
32
  const plan = [];
30
33
  for (const componentName of componentNames) {
31
- const destination = getComponentDestination(variant, componentName, projectConfigPath);
34
+ const destination = getComponentDestination(variantConf, componentName, projectConfigPath);
32
35
  const exists = await pathExists(destination);
36
+ const source = getCachedItemPath({
37
+ bucket: 'systems',
38
+ itemPath: [
39
+ systemConf.name,
40
+ relative(dirname(projectConfigPath), destination),
41
+ ],
42
+ repository: emulsifyConfig.system.repository,
43
+ checkout: emulsifyConfig.system.checkout,
44
+ });
45
+ const { files } = await rewriteComponentIds(source, systemConf.name, emulsifyConfig.project.machineName, { dryRun: true });
33
46
  plan.push({
34
47
  name: componentName,
35
48
  isDependency: Boolean(rootComponentName && componentName !== rootComponentName),
36
49
  destination,
37
50
  exists,
38
51
  action: getDryRunInstallAction(exists, force),
52
+ rewriteFiles: files.map((file) => join(destination, relative(source, file))),
39
53
  });
40
54
  }
41
55
  return plan;
@@ -50,6 +64,12 @@ function logComponentInstallDryRun(targetLabel, dependencies, plan, npmPackages)
50
64
  ` Destination: ${item.destination}`,
51
65
  ` Destination exists: ${item.exists ? 'yes' : 'no'}`,
52
66
  ` Real run would: ${item.action}`,
67
+ ...(item.rewriteFiles.length
68
+ ? [
69
+ ' Component ID rewrites:',
70
+ ...item.rewriteFiles.map((file) => ` - ${file}`),
71
+ ]
72
+ : []),
53
73
  ].join('\n'))
54
74
  .join('\n');
55
75
  log('info', [
@@ -72,7 +92,8 @@ export default async function componentInstall(name, { force, all, dryRun, refre
72
92
  throw new CliError(MISSING_COMPONENT_INSTALL_TARGET_ERROR);
73
93
  requireInteractiveTerminal(MISSING_COMPONENT_INSTALL_TARGET_ERROR);
74
94
  }
75
- const { systemConf, variantConf } = await withEmulsifySystem('install components', { refresh });
95
+ const context = await withEmulsifySystem('install components', { refresh });
96
+ const { systemConf, variantConf } = context;
76
97
  if (needsSelection) {
77
98
  const selection = await runPrompt({
78
99
  prompt: () => select({
@@ -142,7 +163,7 @@ export default async function componentInstall(name, { force, all, dryRun, refre
142
163
  const dependencies = installAll
143
164
  ? []
144
165
  : componentNames.filter((dependency) => dependency !== rootComponentName);
145
- const preview = await buildComponentInstallPlan(variantConf, componentNames, rootComponentName, Boolean(installAll || force), projectConfigPath);
166
+ const preview = await buildComponentInstallPlan(context, componentNames, rootComponentName, Boolean(installAll || force), projectConfigPath);
146
167
  logComponentInstallDryRun(rootComponentName ?? 'all components', dependencies, preview, formatComponentNpmInstall(npmPlan, skipNpm ? 'skip' : 'preview'));
147
168
  return 'Dry run complete';
148
169
  }
@@ -160,9 +181,10 @@ export default async function componentInstall(name, { force, all, dryRun, refre
160
181
  const installedDeps = [];
161
182
  const failedDeps = [];
162
183
  const failureMessages = [];
184
+ let rewritten = 0;
163
185
  for (const [cname, isDependency, promise] of components) {
164
186
  try {
165
- await promise;
187
+ rewritten += await promise;
166
188
  if (isDependency) {
167
189
  installedDeps.push(cname);
168
190
  }
@@ -178,6 +200,7 @@ export default async function componentInstall(name, { force, all, dryRun, refre
178
200
  }
179
201
  }
180
202
  }
203
+ logComponentIdRewrites(rewritten);
181
204
  if (installedDeps.length > 0) {
182
205
  const depList = installedDeps.map((d) => ` → ${d}`).join('\n');
183
206
  note(depList, 'Also installed');
@@ -13,6 +13,7 @@ import getRepositoryLatestTag from '../util/getRepositoryLatestTag.js';
13
13
  import installComponentFromCache from '../util/project/installComponentFromCache.js';
14
14
  import { applyComponentNpmInstall, confirmComponentNpmInstall, formatAddedNpmPackages, getComponentNpmInstallReview, prepareComponentNpmInstall, } from '../util/project/installComponentNpmDependencies.js';
15
15
  import installGeneralAssetsFromCache from '../util/project/installGeneralAssetsFromCache.js';
16
+ import { logComponentIdRewrites } from '../util/project/rewriteComponentIds.js';
16
17
  import getJsonFromCachedFile from '../util/cache/getJsonFromCachedFile.js';
17
18
  import setEmulsifyConfig from '../util/project/setEmulsifyConfig.js';
18
19
  import getEmulsifyConfig from '../util/project/getEmulsifyConfig.js';
@@ -483,10 +484,12 @@ export default async function systemInstall(name, options) {
483
484
  throw new CliError('Unable to update your Emulsify project configuration.');
484
485
  }
485
486
  try {
487
+ let rewritten = 0;
486
488
  for (const component of componentsToInstall) {
487
- await installComponentFromCache(systemConf, variantConf, component.name, true);
489
+ rewritten += await installComponentFromCache(systemConf, variantConf, component.name, true);
488
490
  }
489
- await installGeneralAssetsFromCache(systemConf, variantConf);
491
+ rewritten += await installGeneralAssetsFromCache(systemConf, variantConf);
492
+ logComponentIdRewrites(rewritten);
490
493
  added = await applyComponentNpmInstall(npmPlan, acceptedNpm);
491
494
  const hookPath = projectConfigPath
492
495
  ? join(dirname(projectConfigPath), EMULSIFY_PROJECT_HOOK_FOLDER, EMULSIFY_PROJECT_HOOK_SYSTEM_INSTALL)
@@ -95,6 +95,17 @@
95
95
  },
96
96
  "additionalProperties": false
97
97
  },
98
+ "storybook": {
99
+ "type": "object",
100
+ "description": "Configures Emulsify Core's Storybook integration.",
101
+ "properties": {
102
+ "registerDrupalTwigFilters": {
103
+ "type": "boolean",
104
+ "description": "Whether Emulsify Core registers Drupal Twig filters in Storybook."
105
+ }
106
+ },
107
+ "additionalProperties": true
108
+ },
98
109
  "system": {
99
110
  "type": "object",
100
111
  "description": "Contains information about the Emulsify system this project is utilizing",
@@ -1,6 +1,7 @@
1
1
  import { copy, remove } from 'fs-extra';
2
2
  import getCachedItemPath from './getCachedItemPath.js';
3
3
  import getEmulsifyConfig from '../project/getEmulsifyConfig.js';
4
+ import rewriteComponentIds from '../project/rewriteComponentIds.js';
4
5
  export default async function copyFileFromCache(bucket, itemPath, destination, force = false) {
5
6
  const emulsifyConfig = await getEmulsifyConfig();
6
7
  const repository = emulsifyConfig?.system?.repository || '';
@@ -14,5 +15,10 @@ export default async function copyFileFromCache(bucket, itemPath, destination, f
14
15
  if (force) {
15
16
  await remove(destination);
16
17
  }
17
- return copy(source, destination);
18
+ await copy(source, destination);
19
+ const [systemName] = itemPath;
20
+ if (bucket !== 'systems' || !systemName || !emulsifyConfig)
21
+ return 0;
22
+ const { count } = await rewriteComponentIds(destination, systemName, emulsifyConfig.project.machineName);
23
+ return count;
18
24
  }
@@ -16,5 +16,6 @@ export default async function installGeneralAssetsFromCache(system, variant) {
16
16
  const destination = safeResolveWithin(projectRoot, asset.destinationPath, 'General asset destination');
17
17
  promises.push(catchLater(copyItemFromCache('systems', [system.name, asset.path], destination, true)));
18
18
  }
19
- await Promise.all(promises);
19
+ const counts = await Promise.all(promises);
20
+ return counts.reduce((total, count) => total + count, 0);
20
21
  }
@@ -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
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@emulsify/cli",
3
3
  "productName": "Emulsify CLI",
4
- "version": "2.5.0",
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>",