@emulsify/cli 2.3.1 → 2.4.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.
- package/README.md +96 -16
- package/dist/handlers/componentCreate.js +37 -5
- package/dist/handlers/componentEjectTemplates.js +317 -0
- package/dist/handlers/componentInstall.js +54 -14
- package/dist/handlers/hofs/withEmulsifySystem.js +1 -1
- package/dist/handlers/init.js +75 -32
- package/dist/handlers/systemCreate.js +222 -0
- package/dist/handlers/systemDetach.js +50 -0
- package/dist/handlers/systemInstall.js +339 -101
- package/dist/index.js +67 -110
- package/dist/lib/log.js +12 -9
- package/dist/lib/rootHelp.js +321 -0
- package/dist/lib/terminalColors.js +10 -0
- package/dist/util/cache/cloneIntoCache.js +2 -4
- package/dist/util/deriveComponentNames.js +5 -0
- package/dist/util/deriveCustomElementTagName.js +25 -0
- package/dist/util/fs/writeToJsonFile.js +21 -5
- package/dist/util/getGitRepoNameFromUrl.js +47 -3
- package/dist/util/getNonInteractiveGitEnvironment.js +7 -0
- package/dist/util/getRepositoryLatestTag.js +17 -2
- package/dist/util/platform/platformCompatibility.js +18 -7
- package/dist/util/project/componentTemplates/buildComponentArtifacts.js +121 -0
- package/dist/util/project/componentTemplates/index.js +5 -0
- package/dist/util/project/componentTemplates/react.js +20 -0
- package/dist/util/project/componentTemplates/reactStories.js +18 -0
- package/dist/util/project/componentTemplates/scss.js +2 -3
- package/dist/util/project/componentTemplates/sdcStories.js +2 -2
- package/dist/util/project/componentTemplates/stories.js +2 -2
- package/dist/util/project/componentTemplates/twig.js +2 -3
- package/dist/util/project/componentTemplates/webComponent.js +49 -0
- package/dist/util/project/componentTemplates/webComponentStories.js +19 -0
- package/dist/util/project/componentTypes.js +71 -0
- package/dist/util/project/generateComponent.js +139 -101
- package/dist/util/project/renderTemplate.js +24 -4
- package/dist/util/project/resolveComponentTemplate.js +24 -11
- package/dist/util/project/unsetEmulsifyConfig.js +16 -0
- package/dist/util/prompt/index.js +25 -0
- package/dist/util/system/buildSystemInstallPlan.js +53 -0
- package/dist/util/system/buildSystemScaffold.js +99 -0
- package/dist/util/system/getAvailableSystems.js +4 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
|
|
4
4
|
# Emulsify CLI
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
Build and use component systems in Drupal, WordPress, or standalone front ends.
|
|
7
7
|
|
|
8
8
|
## Requirements
|
|
9
9
|
|
|
@@ -34,7 +34,7 @@ cd ./web/themes/custom/my_theme
|
|
|
34
34
|
emulsify system install
|
|
35
35
|
emulsify component list
|
|
36
36
|
emulsify component install card
|
|
37
|
-
emulsify component create promo-card --directory molecules --
|
|
37
|
+
emulsify component create promo-card --directory molecules --type twig
|
|
38
38
|
```
|
|
39
39
|
|
|
40
40
|
Built-in platforms are `drupal`, `wordpress`, and `none`. For WordPress child themes, use the WordPress platform and starter:
|
|
@@ -45,14 +45,91 @@ emulsify init "My Theme" ./wp-content/themes --platform wordpress
|
|
|
45
45
|
|
|
46
46
|
When WordPress is auto-detected, Emulsify initializes child themes into the detected themes directory, such as `wp-content/themes/my-theme` or `web/app/themes/my-theme` for Bedrock.
|
|
47
47
|
|
|
48
|
-
|
|
48
|
+
To author a standalone, distributable component system, run `system create`
|
|
49
|
+
outside or inside any project. The target directory is created beneath the
|
|
50
|
+
selected parent directory:
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
emulsify system create "My System" --directory ./systems --platform "drupal || wordpress" --git
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
This creates `./systems/my-system` with valid system and variant configuration,
|
|
57
|
+
an installable `example-card` component, repository documentation, a
|
|
58
|
+
`.gitignore`, and a license placeholder to replace before distribution.
|
|
59
|
+
Unless overridden, its required URL metadata uses obvious, schema-valid
|
|
60
|
+
`https://TODO.invalid/...` placeholders that must also be replaced before
|
|
61
|
+
publishing.
|
|
62
|
+
|
|
63
|
+
Add `--dry-run` to preview the normalized target, every generated file, and
|
|
64
|
+
whether Git would be initialized without changing the filesystem.
|
|
65
|
+
|
|
66
|
+
When components installed from another system have evolved into the basis of
|
|
67
|
+
your own, detach the configured system before authoring a replacement:
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
emulsify system detach
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Detaching removes only the `system` and `variant` entries from
|
|
74
|
+
`project.emulsify.json`. Components, project assets, and the cached system
|
|
75
|
+
repository stay in place. Run `system create` to scaffold a new system
|
|
76
|
+
repository, then move or copy the preserved components into that scaffold and
|
|
77
|
+
update `system.emulsify.json`; `system create` does not import them
|
|
78
|
+
automatically.
|
|
79
|
+
|
|
80
|
+
Interactive terminals can run `emulsify component create` with no arguments to
|
|
81
|
+
walk through the component name, type, and directory prompts. The type picker
|
|
82
|
+
always offers Twig, offers Twig SDC in Drupal projects, and offers React and Web
|
|
83
|
+
Component scaffolds when the project's `package.json` declares
|
|
84
|
+
`@emulsify/core`. When a choice is unavailable, the wizard explains why; when
|
|
85
|
+
Twig is the only suitable choice, it skips the one-item prompt. Likewise,
|
|
86
|
+
`emulsify component install` with no name presents the components available in
|
|
87
|
+
the installed system variant plus an explicit choice to install all components.
|
|
88
|
+
|
|
89
|
+
To customize component scaffolds, copy the CLI's built-in templates into the
|
|
90
|
+
project, then edit the resulting files under `.cli/templates/`:
|
|
91
|
+
|
|
92
|
+
```bash
|
|
93
|
+
emulsify component eject-templates twig
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
Run the command without a type in an interactive terminal to select one or more
|
|
97
|
+
component types. Use `--all` to eject every type non-interactively. Existing
|
|
98
|
+
overrides are protected unless `--force` is passed.
|
|
99
|
+
|
|
100
|
+
Prompts only run when standard input is a TTY. In CI, scripts, and commands with
|
|
101
|
+
piped or redirected input, provide every required positional argument and flag;
|
|
102
|
+
the CLI exits with an actionable error instead of waiting for input:
|
|
49
103
|
|
|
50
104
|
```bash
|
|
51
105
|
emulsify init "My Theme" ./web/themes/custom --platform drupal --yes
|
|
106
|
+
emulsify system create my-system --directory ./systems --platform none --git
|
|
107
|
+
emulsify system create my-system --directory ./systems --platform none --git --dry-run
|
|
52
108
|
emulsify system install compound
|
|
53
|
-
emulsify component
|
|
109
|
+
emulsify component install card --force
|
|
110
|
+
# Or install every available component:
|
|
111
|
+
emulsify component install --all
|
|
112
|
+
emulsify component create promo-card --directory molecules --type twig --force
|
|
113
|
+
emulsify component create card --directory molecules --type web-component --tag-name acme-card
|
|
114
|
+
emulsify component eject-templates --all
|
|
115
|
+
emulsify system detach --yes
|
|
54
116
|
```
|
|
55
117
|
|
|
118
|
+
For component installation, provide either a component name or `--all`, and use
|
|
119
|
+
`--force` when an existing destination should be replaced. For component
|
|
120
|
+
creation, provide the positional name plus `--type` and `--directory`, and use
|
|
121
|
+
`--force` when an existing generated component should be replaced. The existing
|
|
122
|
+
`-y, --yes` form remains available as a compatibility alias. Explicit
|
|
123
|
+
`--type` values are honored even when project detection would hide that choice
|
|
124
|
+
from the wizard. The deprecated `--format default` and `--format sdc` forms
|
|
125
|
+
remain available as aliases for `--type twig` and `--type twig-sdc`,
|
|
126
|
+
respectively, and print a deprecation warning. Web Components derive their tag
|
|
127
|
+
name from the component and project names; pass `--tag-name` to override it,
|
|
128
|
+
including when the derived value would be invalid in a non-interactive run.
|
|
129
|
+
For template ejection, provide the component type or `--all` outside a TTY; use
|
|
130
|
+
`--dry-run` to preview paths and `--force` only when existing customizations
|
|
131
|
+
should be replaced.
|
|
132
|
+
|
|
56
133
|
## Documentation
|
|
57
134
|
|
|
58
135
|
Detailed documentation lives in [docs](./docs/README.md).
|
|
@@ -61,26 +138,29 @@ Detailed documentation lives in [docs](./docs/README.md).
|
|
|
61
138
|
| ---------------------------------------------------------------------- | ------------------------------------------------------------------------------- |
|
|
62
139
|
| [CLI Reference](./docs/cli-reference.md) | Looking up commands, aliases, options, and examples. |
|
|
63
140
|
| [Project Initialization](./docs/project-initialization.md) | Creating a new Emulsify project from a starter. |
|
|
64
|
-
| [Systems](./docs/systems.md) | Listing, installing, or authoring component systems.
|
|
141
|
+
| [Systems](./docs/systems.md) | Listing, installing, detaching, or authoring component systems. |
|
|
65
142
|
| [Components](./docs/components.md) | Listing, installing, dry-running, or creating components. |
|
|
66
143
|
| [Project Configuration](./docs/configuration.md) | Understanding `project.emulsify.json`, variants, and structure mappings. |
|
|
67
|
-
| [Component Template Overrides](./docs/component-template-overrides.md) |
|
|
144
|
+
| [Component Template Overrides](./docs/component-template-overrides.md) | Ejecting and customizing files used by `emulsify component create`. |
|
|
68
145
|
| [Hooks And Cache](./docs/hooks-and-cache.md) | Understanding starter hooks, system hooks, and local repository cache behavior. |
|
|
69
146
|
| [Development](./docs/development.md) | Setting up this repository and running local checks. |
|
|
70
147
|
| [Release](./docs/release.md) | Understanding CI, semantic-release, and npm publishing. |
|
|
71
148
|
|
|
72
149
|
## Command Overview
|
|
73
150
|
|
|
74
|
-
| Command
|
|
75
|
-
|
|
|
76
|
-
| `emulsify init [name] [path]`
|
|
77
|
-
| `emulsify audit [...args]`
|
|
78
|
-
| `emulsify system list`
|
|
79
|
-
| `emulsify system
|
|
80
|
-
| `emulsify
|
|
81
|
-
| `emulsify
|
|
82
|
-
| `emulsify component
|
|
83
|
-
| `emulsify
|
|
151
|
+
| Command | Alias | Description |
|
|
152
|
+
| ------------------------------------------- | ----------------------------- | ----------------------------------------------------------------- |
|
|
153
|
+
| `emulsify init [name] [path]` | | Initializes an Emulsify project from a starter. |
|
|
154
|
+
| `emulsify audit [...args]` | | Runs the project-installed Emulsify Core audit. |
|
|
155
|
+
| `emulsify system list` | `emulsify system ls` | Lists built-in systems available for installation. |
|
|
156
|
+
| `emulsify system create [name]` | | Creates a standalone component-system repository. |
|
|
157
|
+
| `emulsify system install [name]` | | Installs a system in the current Emulsify project. |
|
|
158
|
+
| `emulsify system detach` | | Detaches the system and keeps project components. |
|
|
159
|
+
| `emulsify component list` | `emulsify component ls` | Lists components available from the installed system and variant. |
|
|
160
|
+
| `emulsify component install [name]` | `emulsify component i [name]` | Installs one component from the installed system and variant. |
|
|
161
|
+
| `emulsify component create [name]` | `emulsify component c [name]` | Creates a local component in the current Emulsify project. |
|
|
162
|
+
| `emulsify component eject-templates [type]` | | Writes editable built-in templates into the current project. |
|
|
163
|
+
| `emulsify cache clear` | | Clears locally cached system repositories. |
|
|
84
164
|
|
|
85
165
|
`emulsify audit` is a convenience façade. The project-installed
|
|
86
166
|
`@emulsify/core` package remains the owner of the canonical `emulsify-audit`
|
|
@@ -1,18 +1,50 @@
|
|
|
1
|
+
import { input } from '@inquirer/prompts';
|
|
1
2
|
import generateComponent from '../util/project/generateComponent.js';
|
|
2
3
|
import { withEmulsifySystem } from './hofs/withEmulsifySystem.js';
|
|
3
4
|
import CliError from '../lib/CliError.js';
|
|
5
|
+
import deriveComponentNames from '../util/deriveComponentNames.js';
|
|
6
|
+
import { isExitPromptError, requireInteractiveTerminal, runPrompt, } from '../util/prompt/index.js';
|
|
7
|
+
import { MISSING_COMPONENT_DIRECTORY_ERROR, MISSING_COMPONENT_TYPE_ERROR, } from '../util/project/componentTypes.js';
|
|
8
|
+
const MISSING_COMPONENT_NAME_ERROR = 'Please specify a name for the new component.';
|
|
9
|
+
function validateComponentName(name) {
|
|
10
|
+
try {
|
|
11
|
+
deriveComponentNames(name);
|
|
12
|
+
return true;
|
|
13
|
+
}
|
|
14
|
+
catch (error) {
|
|
15
|
+
return error.message;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
4
18
|
export default async function componentCreate(name, options = {}) {
|
|
5
|
-
|
|
6
|
-
|
|
19
|
+
const providedComponentName = name?.trim() ? name : undefined;
|
|
20
|
+
if (!providedComponentName) {
|
|
21
|
+
requireInteractiveTerminal(MISSING_COMPONENT_NAME_ERROR);
|
|
22
|
+
}
|
|
23
|
+
if (!options.type && !options.format) {
|
|
24
|
+
requireInteractiveTerminal(MISSING_COMPONENT_TYPE_ERROR);
|
|
25
|
+
}
|
|
26
|
+
if (!options.directory) {
|
|
27
|
+
requireInteractiveTerminal(MISSING_COMPONENT_DIRECTORY_ERROR);
|
|
7
28
|
}
|
|
8
|
-
const { variantConf } = await withEmulsifySystem('create components', {
|
|
29
|
+
const { emulsifyConfig, variantConf } = await withEmulsifySystem('create components', {
|
|
9
30
|
refresh: options.refresh,
|
|
10
31
|
});
|
|
32
|
+
const componentName = providedComponentName ??
|
|
33
|
+
(await runPrompt({
|
|
34
|
+
prompt: () => input({
|
|
35
|
+
message: 'Component name:',
|
|
36
|
+
validate: validateComponentName,
|
|
37
|
+
}),
|
|
38
|
+
nonInteractive: { error: MISSING_COMPONENT_NAME_ERROR },
|
|
39
|
+
}));
|
|
11
40
|
try {
|
|
12
|
-
await generateComponent(variantConf,
|
|
41
|
+
await generateComponent(variantConf, emulsifyConfig, componentName, options);
|
|
13
42
|
}
|
|
14
43
|
catch (e) {
|
|
44
|
+
if (isExitPromptError(e)) {
|
|
45
|
+
throw e;
|
|
46
|
+
}
|
|
15
47
|
const msg = e instanceof Error ? e.message : String(e);
|
|
16
|
-
throw new CliError(`Unable to create the ${
|
|
48
|
+
throw new CliError(`Unable to create the ${componentName} component: ${msg}`);
|
|
17
49
|
}
|
|
18
50
|
}
|
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
import { checkbox } from '@inquirer/prompts';
|
|
2
|
+
import { randomUUID } from 'crypto';
|
|
3
|
+
import { constants as fsConstants, promises as fs } from 'fs';
|
|
4
|
+
import { basename, dirname, join } from 'path';
|
|
5
|
+
import { pathExists } from 'fs-extra';
|
|
6
|
+
import CliError from '../lib/CliError.js';
|
|
7
|
+
import { EMULSIFY_PROJECT_CONFIG_FILE, EMULSIFY_PROJECT_TEMPLATES_FOLDER, } from '../lib/constants.js';
|
|
8
|
+
import log from '../lib/log.js';
|
|
9
|
+
import findFileInCurrentPath from '../util/fs/findFileInCurrentPath.js';
|
|
10
|
+
import safeResolveWithin from '../util/fs/safeResolveWithin.js';
|
|
11
|
+
import { COMPONENT_TYPES, normalizeComponentType, } from '../util/project/componentTypes.js';
|
|
12
|
+
import { buildEjectableComponentTemplates } from '../util/project/componentTemplates/index.js';
|
|
13
|
+
import { requireInteractiveTerminal, runPrompt } from '../util/prompt/index.js';
|
|
14
|
+
export const MISSING_TEMPLATE_TYPE_ERROR = 'Component template selection is required in non-interactive mode. Pass the [type] positional argument (twig, twig-sdc, react, or web-component), or pass --all.';
|
|
15
|
+
export const CONFLICTING_TEMPLATE_TYPE_ERROR = 'Pass either the [type] positional argument or --all, not both.';
|
|
16
|
+
const TYPE_CHOICES = [
|
|
17
|
+
{
|
|
18
|
+
name: 'Twig',
|
|
19
|
+
value: 'twig',
|
|
20
|
+
description: 'Twig markup, SCSS, YAML data, and a Storybook story',
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
name: 'Twig SDC',
|
|
24
|
+
value: 'twig-sdc',
|
|
25
|
+
description: 'Drupal Single Directory Component templates',
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
name: 'React',
|
|
29
|
+
value: 'react',
|
|
30
|
+
description: 'React JSX, SCSS, and a standard Storybook story',
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
name: 'Web Component',
|
|
34
|
+
value: 'web-component',
|
|
35
|
+
description: 'Custom element, SCSS, and an Emulsify Core story',
|
|
36
|
+
},
|
|
37
|
+
];
|
|
38
|
+
export function buildComponentTemplateEjectionPlan(projectRoot, types) {
|
|
39
|
+
const templatesRoot = safeResolveWithin(projectRoot, EMULSIFY_PROJECT_TEMPLATES_FOLDER, 'Component templates directory');
|
|
40
|
+
return types.flatMap((type) => {
|
|
41
|
+
safeResolveWithin(templatesRoot, type, 'Component template type directory');
|
|
42
|
+
return buildEjectableComponentTemplates(type).map(({ logicalName, contents }) => ({
|
|
43
|
+
type,
|
|
44
|
+
logicalName,
|
|
45
|
+
destination: safeResolveWithin(templatesRoot, [type, logicalName], 'Component template destination'),
|
|
46
|
+
contents,
|
|
47
|
+
}));
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
function normalizeRequestedType(type) {
|
|
51
|
+
try {
|
|
52
|
+
return normalizeComponentType(type);
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
throw new CliError(error instanceof Error ? error.message : String(error));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
function canonicalizeSelectedTypes(selectedTypes) {
|
|
59
|
+
const selected = new Set(selectedTypes);
|
|
60
|
+
return COMPONENT_TYPES.filter((type) => selected.has(type));
|
|
61
|
+
}
|
|
62
|
+
async function inspectPlan(plan) {
|
|
63
|
+
return Promise.all(plan.map(async (item) => ({
|
|
64
|
+
...item,
|
|
65
|
+
exists: await pathExists(item.destination),
|
|
66
|
+
})));
|
|
67
|
+
}
|
|
68
|
+
function getDryRunAction(exists, force) {
|
|
69
|
+
if (!exists)
|
|
70
|
+
return 'would create';
|
|
71
|
+
if (force)
|
|
72
|
+
return 'would replace';
|
|
73
|
+
return 'conflict; a real run requires --force';
|
|
74
|
+
}
|
|
75
|
+
function logDryRun(types, plan, force) {
|
|
76
|
+
const destinations = plan
|
|
77
|
+
.map(({ destination, exists }) => ` - ${destination} (${getDryRunAction(exists, force)})`)
|
|
78
|
+
.join('\n');
|
|
79
|
+
log('info', [
|
|
80
|
+
'Dry run: component eject-templates',
|
|
81
|
+
`Types: ${types.join(', ')}`,
|
|
82
|
+
'Template files:',
|
|
83
|
+
destinations,
|
|
84
|
+
'No files were written or replaced.',
|
|
85
|
+
].join('\n'));
|
|
86
|
+
}
|
|
87
|
+
function formatConflictError(conflicts) {
|
|
88
|
+
const paths = conflicts
|
|
89
|
+
.map(({ destination }) => ` - ${destination}`)
|
|
90
|
+
.join('\n');
|
|
91
|
+
return [
|
|
92
|
+
'Refusing to overwrite existing component template files:',
|
|
93
|
+
paths,
|
|
94
|
+
'Pass --force to replace the conflicting templates. No files were written.',
|
|
95
|
+
].join('\n');
|
|
96
|
+
}
|
|
97
|
+
function getErrorMessage(error) {
|
|
98
|
+
return error instanceof Error ? error.message : String(error);
|
|
99
|
+
}
|
|
100
|
+
function isAlreadyExistsError(error) {
|
|
101
|
+
return (error !== null &&
|
|
102
|
+
typeof error === 'object' &&
|
|
103
|
+
'code' in error &&
|
|
104
|
+
error.code === 'EEXIST');
|
|
105
|
+
}
|
|
106
|
+
function isMissingError(error) {
|
|
107
|
+
return (error !== null &&
|
|
108
|
+
typeof error === 'object' &&
|
|
109
|
+
'code' in error &&
|
|
110
|
+
error.code === 'ENOENT');
|
|
111
|
+
}
|
|
112
|
+
function isUnsupportedHardLinkError(error) {
|
|
113
|
+
return (error !== null &&
|
|
114
|
+
typeof error === 'object' &&
|
|
115
|
+
'code' in error &&
|
|
116
|
+
(error.code === 'EPERM' ||
|
|
117
|
+
error.code === 'ENOTSUP' ||
|
|
118
|
+
error.code === 'EOPNOTSUPP'));
|
|
119
|
+
}
|
|
120
|
+
function getTransactionPath(destination, kind) {
|
|
121
|
+
return join(dirname(destination), `.${basename(destination)}.emulsify-${kind}-${randomUUID()}`);
|
|
122
|
+
}
|
|
123
|
+
function buildTransactionPlan(plan) {
|
|
124
|
+
return plan.map((item) => ({
|
|
125
|
+
...item,
|
|
126
|
+
temporaryPath: getTransactionPath(item.destination, 'temporary'),
|
|
127
|
+
backupPath: getTransactionPath(item.destination, 'backup'),
|
|
128
|
+
hasBackup: false,
|
|
129
|
+
installed: false,
|
|
130
|
+
}));
|
|
131
|
+
}
|
|
132
|
+
async function removePaths(paths) {
|
|
133
|
+
const failures = [];
|
|
134
|
+
for (const path of paths) {
|
|
135
|
+
try {
|
|
136
|
+
await fs.rm(path, { force: true });
|
|
137
|
+
}
|
|
138
|
+
catch (error) {
|
|
139
|
+
failures.push(` - ${path}: ${getErrorMessage(error)}`);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return failures;
|
|
143
|
+
}
|
|
144
|
+
function appendCleanupFailures(message, failures) {
|
|
145
|
+
if (failures.length === 0)
|
|
146
|
+
return message;
|
|
147
|
+
return [
|
|
148
|
+
message,
|
|
149
|
+
'Temporary transaction files could not be removed:',
|
|
150
|
+
...failures,
|
|
151
|
+
].join('\n');
|
|
152
|
+
}
|
|
153
|
+
async function stageTransaction(plan) {
|
|
154
|
+
for (const item of plan) {
|
|
155
|
+
try {
|
|
156
|
+
await fs.mkdir(dirname(item.destination), { recursive: true });
|
|
157
|
+
await fs.writeFile(item.temporaryPath, item.contents, {
|
|
158
|
+
encoding: 'utf-8',
|
|
159
|
+
flag: 'wx',
|
|
160
|
+
flush: true,
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
catch (error) {
|
|
164
|
+
const cleanupFailures = await removePaths(plan.map(({ temporaryPath }) => temporaryPath));
|
|
165
|
+
throw new CliError(appendCleanupFailures(`Unable to stage component template "${item.destination}": ${getErrorMessage(error)}. No destination files were changed.`, cleanupFailures));
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
async function installTransactionItem(item, force) {
|
|
170
|
+
if (!force) {
|
|
171
|
+
try {
|
|
172
|
+
await fs.link(item.temporaryPath, item.destination);
|
|
173
|
+
}
|
|
174
|
+
catch (error) {
|
|
175
|
+
if (!isUnsupportedHardLinkError(error))
|
|
176
|
+
throw error;
|
|
177
|
+
const destinationHandle = await fs.open(item.destination, 'wx');
|
|
178
|
+
item.installed = true;
|
|
179
|
+
try {
|
|
180
|
+
await fs.writeFile(destinationHandle, item.contents, {
|
|
181
|
+
encoding: 'utf-8',
|
|
182
|
+
flush: true,
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
finally {
|
|
186
|
+
await destinationHandle.close();
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
item.installed = true;
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
try {
|
|
193
|
+
await fs.link(item.destination, item.backupPath);
|
|
194
|
+
item.hasBackup = true;
|
|
195
|
+
}
|
|
196
|
+
catch (error) {
|
|
197
|
+
if (!isMissingError(error)) {
|
|
198
|
+
if (!isUnsupportedHardLinkError(error))
|
|
199
|
+
throw error;
|
|
200
|
+
try {
|
|
201
|
+
await fs.copyFile(item.destination, item.backupPath, fsConstants.COPYFILE_EXCL);
|
|
202
|
+
item.hasBackup = true;
|
|
203
|
+
}
|
|
204
|
+
catch (copyError) {
|
|
205
|
+
if (!isMissingError(copyError))
|
|
206
|
+
throw copyError;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
await fs.rename(item.temporaryPath, item.destination);
|
|
211
|
+
item.installed = true;
|
|
212
|
+
}
|
|
213
|
+
async function rollbackTransaction(plan) {
|
|
214
|
+
const failures = [];
|
|
215
|
+
for (const item of [...plan].reverse()) {
|
|
216
|
+
if (item.hasBackup) {
|
|
217
|
+
try {
|
|
218
|
+
await fs.rename(item.backupPath, item.destination);
|
|
219
|
+
item.hasBackup = false;
|
|
220
|
+
item.installed = false;
|
|
221
|
+
}
|
|
222
|
+
catch (error) {
|
|
223
|
+
failures.push(` - Could not restore "${item.destination}"; its previous contents remain at "${item.backupPath}": ${getErrorMessage(error)}`);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
else if (item.installed) {
|
|
227
|
+
try {
|
|
228
|
+
await fs.rm(item.destination, { force: true });
|
|
229
|
+
item.installed = false;
|
|
230
|
+
}
|
|
231
|
+
catch (error) {
|
|
232
|
+
failures.push(` - Could not remove newly installed "${item.destination}": ${getErrorMessage(error)}`);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return failures;
|
|
237
|
+
}
|
|
238
|
+
function formatInstallError(item, error, force, rollbackFailures, cleanupFailures) {
|
|
239
|
+
const failure = !force && isAlreadyExistsError(error)
|
|
240
|
+
? `Component template "${item.destination}" appeared after the overwrite check and was not replaced. Pass --force to replace existing templates.`
|
|
241
|
+
: `Unable to install component template "${item.destination}": ${getErrorMessage(error)}.`;
|
|
242
|
+
const rollback = rollbackFailures.length === 0
|
|
243
|
+
? 'All destination changes were rolled back.'
|
|
244
|
+
: ['Rollback was incomplete:', ...rollbackFailures].join('\n');
|
|
245
|
+
return appendCleanupFailures([failure, rollback].join('\n'), cleanupFailures);
|
|
246
|
+
}
|
|
247
|
+
async function executeTransaction(plan, force) {
|
|
248
|
+
const transactionPlan = buildTransactionPlan(plan);
|
|
249
|
+
await stageTransaction(transactionPlan);
|
|
250
|
+
for (const item of transactionPlan) {
|
|
251
|
+
try {
|
|
252
|
+
await installTransactionItem(item, force);
|
|
253
|
+
}
|
|
254
|
+
catch (error) {
|
|
255
|
+
const rollbackFailures = await rollbackTransaction(transactionPlan);
|
|
256
|
+
const cleanupFailures = await removePaths(transactionPlan.flatMap(({ temporaryPath, backupPath, hasBackup }) => hasBackup ? [temporaryPath] : [temporaryPath, backupPath]));
|
|
257
|
+
throw new CliError(formatInstallError(item, error, force, rollbackFailures, cleanupFailures));
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
const cleanupFailures = await removePaths(transactionPlan.flatMap(({ temporaryPath, backupPath, hasBackup }) => hasBackup ? [temporaryPath, backupPath] : [temporaryPath]));
|
|
261
|
+
if (cleanupFailures.length > 0) {
|
|
262
|
+
throw new CliError([
|
|
263
|
+
'Component templates were installed, but transaction cleanup was incomplete:',
|
|
264
|
+
...cleanupFailures,
|
|
265
|
+
].join('\n'));
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
export default async function componentEjectTemplates(type, { all = false, force = false, dryRun = false, } = {}) {
|
|
269
|
+
const requestedType = type?.trim();
|
|
270
|
+
if (requestedType && all) {
|
|
271
|
+
throw new CliError(CONFLICTING_TEMPLATE_TYPE_ERROR);
|
|
272
|
+
}
|
|
273
|
+
if (!requestedType && !all) {
|
|
274
|
+
requireInteractiveTerminal(MISSING_TEMPLATE_TYPE_ERROR);
|
|
275
|
+
}
|
|
276
|
+
const projectConfigPath = findFileInCurrentPath(EMULSIFY_PROJECT_CONFIG_FILE);
|
|
277
|
+
if (!projectConfigPath) {
|
|
278
|
+
throw new CliError('No Emulsify project detected. Run this command within an existing Emulsify project.');
|
|
279
|
+
}
|
|
280
|
+
const projectRoot = dirname(projectConfigPath);
|
|
281
|
+
let selectedTypes;
|
|
282
|
+
if (all) {
|
|
283
|
+
selectedTypes = COMPONENT_TYPES;
|
|
284
|
+
}
|
|
285
|
+
else if (requestedType) {
|
|
286
|
+
selectedTypes = [normalizeRequestedType(requestedType)];
|
|
287
|
+
}
|
|
288
|
+
else {
|
|
289
|
+
selectedTypes = await runPrompt({
|
|
290
|
+
prompt: () => checkbox({
|
|
291
|
+
message: 'Which component template types should be ejected?',
|
|
292
|
+
choices: TYPE_CHOICES,
|
|
293
|
+
validate: (values) => values.length > 0 || 'Select at least one component type.',
|
|
294
|
+
}),
|
|
295
|
+
nonInteractive: { error: MISSING_TEMPLATE_TYPE_ERROR },
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
const canonicalTypes = canonicalizeSelectedTypes(selectedTypes);
|
|
299
|
+
if (canonicalTypes.length === 0) {
|
|
300
|
+
throw new CliError('Select at least one component type.');
|
|
301
|
+
}
|
|
302
|
+
const inspectedPlan = await inspectPlan(buildComponentTemplateEjectionPlan(projectRoot, canonicalTypes));
|
|
303
|
+
const conflicts = inspectedPlan.filter(({ exists }) => exists);
|
|
304
|
+
if (dryRun) {
|
|
305
|
+
logDryRun(canonicalTypes, inspectedPlan, force);
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
if (conflicts.length > 0 && !force) {
|
|
309
|
+
throw new CliError(formatConflictError(conflicts));
|
|
310
|
+
}
|
|
311
|
+
await executeTransaction(inspectedPlan, force);
|
|
312
|
+
const paths = inspectedPlan
|
|
313
|
+
.map(({ destination }) => ` - ${destination}`)
|
|
314
|
+
.join('\n');
|
|
315
|
+
log('success', `Ejected ${inspectedPlan.length} built-in component templates:\n${paths}`);
|
|
316
|
+
log('info', 'Edit these files to customize component create. Delete an override to restore its built-in template.');
|
|
317
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { pathExists } from 'fs-extra';
|
|
2
|
-
import { confirm } from '@inquirer/prompts';
|
|
2
|
+
import { confirm, select } from '@inquirer/prompts';
|
|
3
3
|
import log from '../lib/log.js';
|
|
4
4
|
import { EMULSIFY_PROJECT_CONFIG_FILE } from '../lib/constants.js';
|
|
5
5
|
import CliError from '../lib/CliError.js';
|
|
@@ -7,7 +7,10 @@ import installComponentFromCache, { getComponentDestination, } from '../util/pro
|
|
|
7
7
|
import buildComponentDependencyList from '../util/project/buildComponentDependencyList.js';
|
|
8
8
|
import catchLater from '../util/catchLater.js';
|
|
9
9
|
import findFileInCurrentPath from '../util/fs/findFileInCurrentPath.js';
|
|
10
|
+
import { requireInteractiveTerminal, runPrompt } from '../util/prompt/index.js';
|
|
10
11
|
import { withEmulsifySystem } from './hofs/withEmulsifySystem.js';
|
|
12
|
+
const INSTALL_ALL_COMPONENTS = Symbol('install all components');
|
|
13
|
+
const MISSING_COMPONENT_INSTALL_TARGET_ERROR = 'Please specify a component to install, or pass --all to install all available components.';
|
|
11
14
|
function getDryRunInstallAction(exists, force) {
|
|
12
15
|
if (!exists) {
|
|
13
16
|
return 'copy component';
|
|
@@ -58,12 +61,40 @@ function logComponentInstallDryRun(targetLabel, dependencies, plan) {
|
|
|
58
61
|
].join('\n'));
|
|
59
62
|
}
|
|
60
63
|
export default async function componentInstall(name, { force, all, dryRun, refresh }) {
|
|
61
|
-
|
|
62
|
-
|
|
64
|
+
let selectedName = name;
|
|
65
|
+
let installAll = all === true;
|
|
66
|
+
const needsSelection = !selectedName?.trim() && !installAll;
|
|
67
|
+
if (needsSelection) {
|
|
68
|
+
requireInteractiveTerminal(MISSING_COMPONENT_INSTALL_TARGET_ERROR);
|
|
63
69
|
}
|
|
64
70
|
const { systemConf, variantConf } = await withEmulsifySystem('install components', { refresh });
|
|
71
|
+
if (needsSelection) {
|
|
72
|
+
const selection = await runPrompt({
|
|
73
|
+
prompt: () => select({
|
|
74
|
+
message: 'Choose a component to install:',
|
|
75
|
+
choices: [
|
|
76
|
+
...variantConf.components.map((component) => ({
|
|
77
|
+
name: component.name,
|
|
78
|
+
value: component.name,
|
|
79
|
+
description: component.description,
|
|
80
|
+
})),
|
|
81
|
+
{
|
|
82
|
+
name: 'Install all available components',
|
|
83
|
+
value: INSTALL_ALL_COMPONENTS,
|
|
84
|
+
},
|
|
85
|
+
],
|
|
86
|
+
}),
|
|
87
|
+
nonInteractive: { error: MISSING_COMPONENT_INSTALL_TARGET_ERROR },
|
|
88
|
+
});
|
|
89
|
+
if (selection === INSTALL_ALL_COMPONENTS) {
|
|
90
|
+
installAll = true;
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
selectedName = selection;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
65
96
|
const components = [];
|
|
66
|
-
if (
|
|
97
|
+
if (installAll) {
|
|
67
98
|
const componentNames = variantConf.components.map((component) => component.name);
|
|
68
99
|
if (dryRun) {
|
|
69
100
|
const plan = await buildComponentInstallPlan(variantConf, componentNames, undefined, true);
|
|
@@ -77,28 +108,37 @@ export default async function componentInstall(name, { force, all, dryRun, refre
|
|
|
77
108
|
]));
|
|
78
109
|
}
|
|
79
110
|
else {
|
|
80
|
-
const
|
|
111
|
+
const rootComponentName = selectedName;
|
|
112
|
+
const componentsWithDependencies = buildComponentDependencyList(variantConf.components, rootComponentName);
|
|
81
113
|
if (componentsWithDependencies.length === 0) {
|
|
82
|
-
throw new CliError(`Cannot find the definition for component "${
|
|
114
|
+
throw new CliError(`Cannot find the definition for component "${rootComponentName}".\n\nRun "emulsify component list" to see the full list.`);
|
|
83
115
|
}
|
|
84
116
|
if (dryRun) {
|
|
85
|
-
const dependencies = componentsWithDependencies.filter((
|
|
86
|
-
const plan = await buildComponentInstallPlan(variantConf, componentsWithDependencies,
|
|
87
|
-
logComponentInstallDryRun(
|
|
117
|
+
const dependencies = componentsWithDependencies.filter((dependencyName) => dependencyName !== rootComponentName);
|
|
118
|
+
const plan = await buildComponentInstallPlan(variantConf, componentsWithDependencies, rootComponentName, Boolean(force));
|
|
119
|
+
logComponentInstallDryRun(rootComponentName, dependencies, plan);
|
|
88
120
|
return;
|
|
89
121
|
}
|
|
90
122
|
const projectConfigPath = findFileInCurrentPath(EMULSIFY_PROJECT_CONFIG_FILE);
|
|
91
123
|
for (const componentName of componentsWithDependencies) {
|
|
92
|
-
const isDependency = componentName !==
|
|
124
|
+
const isDependency = componentName !== rootComponentName;
|
|
93
125
|
let currentForce = force;
|
|
94
126
|
const destination = projectConfigPath
|
|
95
127
|
? getComponentDestination(variantConf, componentName, projectConfigPath)
|
|
96
128
|
: undefined;
|
|
97
129
|
if (destination && (await pathExists(destination)) && !force) {
|
|
98
|
-
const dependencyNote = isDependency
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
130
|
+
const dependencyNote = isDependency
|
|
131
|
+
? ` (required by "${rootComponentName}")`
|
|
132
|
+
: '';
|
|
133
|
+
const overwriteMessage = `The component "${componentName}"${dependencyNote} already exists.`;
|
|
134
|
+
const result = await runPrompt({
|
|
135
|
+
prompt: () => confirm({
|
|
136
|
+
message: `${overwriteMessage} Would you like to replace it?`,
|
|
137
|
+
default: false,
|
|
138
|
+
}),
|
|
139
|
+
nonInteractive: {
|
|
140
|
+
error: `${overwriteMessage} Pass --force to replace existing components in non-interactive mode.`,
|
|
141
|
+
},
|
|
102
142
|
});
|
|
103
143
|
if (result) {
|
|
104
144
|
currentForce = true;
|
|
@@ -82,7 +82,7 @@ export async function withEmulsifySystem(actionLabel, { refresh = false } = {})
|
|
|
82
82
|
validation = await validateSystemConfig(systemConfig);
|
|
83
83
|
}
|
|
84
84
|
if (!validation.valid) {
|
|
85
|
-
throw new EmulsifySystemError(`The cached copy of the ${systemName} system is invalid. Run "emulsify cache clear" and retry this command to re-clone it. To reinstall the system instead,
|
|
85
|
+
throw new EmulsifySystemError(`The cached copy of the ${systemName} system is invalid. Run "emulsify cache clear" and retry this command to re-clone it. To reinstall the system instead, run "emulsify system detach" followed by "emulsify system install".`);
|
|
86
86
|
}
|
|
87
87
|
const systemConf = validation.systemConfig;
|
|
88
88
|
if (skippedPlatformExpressions.length > 0) {
|