@drupal-canvas/create 0.0.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 +79 -0
- package/dist/index.js +241 -0
- package/package.json +49 -0
- package/templates.json +10 -0
package/README.md
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# Drupal Canvas Create
|
|
2
|
+
|
|
3
|
+
CLI to scaffold a codebase for working with Drupal Canvas Code Components.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
Create a new project interactively:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npx @drupal-canvas/create@latest
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
yarn dlx @drupal-canvas/create@latest
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pnpm dlx @drupal-canvas/create@latest
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
bunx @drupal-canvas/create@latest
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
You can also provide the app name as an argument:
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
npx @drupal-canvas/create@latest my-app
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### Options
|
|
32
|
+
|
|
33
|
+
| Option | Description |
|
|
34
|
+
| --------------- | ------------------------------------------------------------------------- |
|
|
35
|
+
| `--template -t` | Template to use when scaffolding the app. Available: `canvas-cc-starter`. |
|
|
36
|
+
|
|
37
|
+
### Example
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
npx @drupal-canvas/create@latest my-app --template canvas-cc-starter
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Development
|
|
44
|
+
|
|
45
|
+
Drupal Canvas Create is designed to be easily extendable with new templates.
|
|
46
|
+
|
|
47
|
+
**Templates** are predefined application starter codebases. Each template
|
|
48
|
+
references a Git repository that will be cloned to provide the initial codebase.
|
|
49
|
+
To add a template, edit `templates.json` in the package root.
|
|
50
|
+
|
|
51
|
+
### Working with the codebase
|
|
52
|
+
|
|
53
|
+
First, build the project:
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
npm run build
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Then you can execute the script locally:
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
npm start
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Alternatively, use `npm run dev` to compile and watch for changes during
|
|
66
|
+
development.
|
|
67
|
+
|
|
68
|
+
⚠️ You must use `my-canvas-app` (provided as default value) as your app name
|
|
69
|
+
when running the script from a local directory. (Reasons are explained in the
|
|
70
|
+
`.gitignore` file where we had to ignore this directory.)
|
|
71
|
+
|
|
72
|
+
### Scripts
|
|
73
|
+
|
|
74
|
+
| Command | Description |
|
|
75
|
+
| ------------ | ------------------------------------------------------------------------ |
|
|
76
|
+
| `start` | Run the compiled CLI tool from the `dist` folder. |
|
|
77
|
+
| `dev` | Compile to the `dist` folder for development while watching for changes. |
|
|
78
|
+
| `build` | Compile to the `dist` folder for production use. |
|
|
79
|
+
| `type-check` | Run TypeScript type checking without emitting files. |
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import chalk from 'chalk';
|
|
3
|
+
import { Command } from 'commander';
|
|
4
|
+
import * as p2 from '@clack/prompts';
|
|
5
|
+
import { readFile, writeFile } from 'fs/promises';
|
|
6
|
+
import { join } from 'path';
|
|
7
|
+
import spawn from 'cross-spawn';
|
|
8
|
+
import { rimraf } from 'rimraf';
|
|
9
|
+
import { simpleGit } from 'simple-git';
|
|
10
|
+
import validate from 'validate-npm-package-name';
|
|
11
|
+
|
|
12
|
+
// templates.json
|
|
13
|
+
var templates_default = [
|
|
14
|
+
{
|
|
15
|
+
id: "canvas-cc-starter",
|
|
16
|
+
label: "canvas-cc-starter",
|
|
17
|
+
repository: {
|
|
18
|
+
url: "git@github.com:balintbrews/canvas-cc-starter.git",
|
|
19
|
+
ref: "main"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
id: "canvas-cc-starter-2",
|
|
24
|
+
label: "canvas-cc-starter-2",
|
|
25
|
+
repository: {
|
|
26
|
+
url: "git@github.com:balintbrews/canvas-cc-starter.git",
|
|
27
|
+
ref: "main"
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
];
|
|
31
|
+
|
|
32
|
+
// src/lib/detect-package-manager.ts
|
|
33
|
+
function detectPackageManager() {
|
|
34
|
+
const userAgent = process.env.npm_config_user_agent || "";
|
|
35
|
+
if (userAgent.startsWith("yarn")) {
|
|
36
|
+
return "yarn";
|
|
37
|
+
}
|
|
38
|
+
if (userAgent.startsWith("pnpm")) {
|
|
39
|
+
return "pnpm";
|
|
40
|
+
}
|
|
41
|
+
if (userAgent.startsWith("bun")) {
|
|
42
|
+
return "bun";
|
|
43
|
+
}
|
|
44
|
+
return "npm";
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// package.json
|
|
48
|
+
var package_default = {
|
|
49
|
+
name: "@drupal-canvas/create",
|
|
50
|
+
version: "0.0.1",
|
|
51
|
+
description: "CLI to scaffold a codebase for working with Drupal Canvas Code Components"};
|
|
52
|
+
|
|
53
|
+
// src/lib/meta-info.ts
|
|
54
|
+
var getName = () => package_default.name;
|
|
55
|
+
var getDescription = () => package_default.description;
|
|
56
|
+
var getVersion = () => package_default.version;
|
|
57
|
+
|
|
58
|
+
// src/create.ts
|
|
59
|
+
async function createApp(ctx) {
|
|
60
|
+
const { template, appName } = ctx;
|
|
61
|
+
try {
|
|
62
|
+
const s1 = p2.spinner();
|
|
63
|
+
s1.start("Fetching initial codebase");
|
|
64
|
+
const git = simpleGit();
|
|
65
|
+
await git.clone(template.repository.url, appName, {
|
|
66
|
+
"--depth": 1
|
|
67
|
+
});
|
|
68
|
+
const gitAppDir = simpleGit({
|
|
69
|
+
baseDir: `${process.cwd()}/${appName}`
|
|
70
|
+
});
|
|
71
|
+
await gitAppDir.checkout(template.repository.ref);
|
|
72
|
+
await rimraf(`${process.cwd()}/${appName}/.git`);
|
|
73
|
+
const packageJsonPath = join(process.cwd(), appName, "package.json");
|
|
74
|
+
const packageJsonContent = await readFile(packageJsonPath, "utf-8");
|
|
75
|
+
const packageJson = JSON.parse(packageJsonContent);
|
|
76
|
+
packageJson.name = appName;
|
|
77
|
+
await writeFile(
|
|
78
|
+
packageJsonPath,
|
|
79
|
+
JSON.stringify(packageJson, null, 2) + "\n"
|
|
80
|
+
);
|
|
81
|
+
s1.stop(chalk.green("Fetched initial codebase"));
|
|
82
|
+
const s2 = p2.spinner();
|
|
83
|
+
const packageManager = detectPackageManager();
|
|
84
|
+
s2.start(`Installing dependencies using ${packageManager}`);
|
|
85
|
+
await new Promise((resolve, reject) => {
|
|
86
|
+
const child = spawn(packageManager, ["install"], {
|
|
87
|
+
cwd: `./${appName}`,
|
|
88
|
+
stdio: ["ignore", "ignore", "pipe"],
|
|
89
|
+
env: {
|
|
90
|
+
...process.env,
|
|
91
|
+
NODE_ENV: "development",
|
|
92
|
+
ADBLOCK: "1",
|
|
93
|
+
DISABLE_OPENCOLLECTIVE: "1"
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
let stderrOutput = "";
|
|
97
|
+
if (child.stderr) {
|
|
98
|
+
child.stderr.on("data", (data) => {
|
|
99
|
+
stderrOutput += data.toString();
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
child.on("close", (code) => {
|
|
103
|
+
if (code !== 0) {
|
|
104
|
+
reject(
|
|
105
|
+
new Error(
|
|
106
|
+
`Package installation failed with code ${code}${stderrOutput ? `:
|
|
107
|
+
${stderrOutput}` : ""}`
|
|
108
|
+
)
|
|
109
|
+
);
|
|
110
|
+
} else {
|
|
111
|
+
resolve();
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
s2.stop(chalk.green("Installed dependencies"));
|
|
116
|
+
const s3 = p2.spinner();
|
|
117
|
+
s3.start("Preparing your repository");
|
|
118
|
+
await git.init(["--initial-branch=main", appName]);
|
|
119
|
+
await gitAppDir.add(["--all"]);
|
|
120
|
+
await gitAppDir.commit(
|
|
121
|
+
`Init codebase using ${getName()}@${getVersion()}
|
|
122
|
+
|
|
123
|
+
Template repository: ${template.repository.url}
|
|
124
|
+
Ref: ${template.repository.ref}`
|
|
125
|
+
);
|
|
126
|
+
s3.stop(chalk.green("Prepared repository"));
|
|
127
|
+
p2.note(`$ cd ${appName}
|
|
128
|
+
$ ${packageManager} run dev`, "Get started");
|
|
129
|
+
p2.outro("\u{1F680} App created successfully");
|
|
130
|
+
} catch (error) {
|
|
131
|
+
if (error instanceof Error) {
|
|
132
|
+
p2.log.error(`Error: ${error.message}`);
|
|
133
|
+
} else {
|
|
134
|
+
p2.log.error(`Unknown error: ${String(error)}`);
|
|
135
|
+
}
|
|
136
|
+
process.exit(1);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
function validateName(name) {
|
|
140
|
+
const nameValidation = validate(name);
|
|
141
|
+
if (nameValidation.validForNewPackages) {
|
|
142
|
+
return { valid: true, problems: [] };
|
|
143
|
+
}
|
|
144
|
+
return {
|
|
145
|
+
valid: false,
|
|
146
|
+
problems: [
|
|
147
|
+
...nameValidation.errors || [],
|
|
148
|
+
...nameValidation.warnings || []
|
|
149
|
+
]
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// src/index.ts
|
|
154
|
+
process.on("SIGINT", () => process.exit(0));
|
|
155
|
+
process.on("SIGTERM", () => process.exit(0));
|
|
156
|
+
var program = new Command();
|
|
157
|
+
program.name(getName()).description(getDescription()).version(getVersion()).argument("[app-name]", "Name of the app to create").option(
|
|
158
|
+
"-t, --template <template>",
|
|
159
|
+
"Template to use when scaffolding the app"
|
|
160
|
+
).action(async (appNameArg, options) => {
|
|
161
|
+
p2.intro(chalk.bold("Drupal Canvas Create"));
|
|
162
|
+
try {
|
|
163
|
+
if (options.template) {
|
|
164
|
+
const template2 = templates_default.find(
|
|
165
|
+
(t) => t.id === options.template
|
|
166
|
+
);
|
|
167
|
+
if (!template2) {
|
|
168
|
+
p2.log.error(`Template "${options.template}" not found`);
|
|
169
|
+
process.exit(1);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
let appName = appNameArg;
|
|
173
|
+
if (!appName) {
|
|
174
|
+
const name = await p2.text({
|
|
175
|
+
message: "Enter the app name",
|
|
176
|
+
initialValue: "my-canvas-app",
|
|
177
|
+
validate: (value) => {
|
|
178
|
+
if (!value) return "App name is required";
|
|
179
|
+
const { valid, problems } = validateName(value);
|
|
180
|
+
if (!valid) {
|
|
181
|
+
return problems.join(", ");
|
|
182
|
+
}
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
if (p2.isCancel(name)) {
|
|
187
|
+
p2.cancel("Operation cancelled");
|
|
188
|
+
process.exit(0);
|
|
189
|
+
}
|
|
190
|
+
appName = name;
|
|
191
|
+
} else {
|
|
192
|
+
const { valid, problems } = validateName(appName);
|
|
193
|
+
if (!valid) {
|
|
194
|
+
p2.log.error(`Invalid app name: ${problems.join(", ")}`);
|
|
195
|
+
process.exit(1);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
let templateId = options.template;
|
|
199
|
+
if (!templateId) {
|
|
200
|
+
if (templates_default.length === 1) {
|
|
201
|
+
templateId = templates_default[0].id;
|
|
202
|
+
} else {
|
|
203
|
+
const selected = await p2.select({
|
|
204
|
+
message: "Select a template",
|
|
205
|
+
options: templates_default.map((t) => ({
|
|
206
|
+
value: t.id,
|
|
207
|
+
label: t.label
|
|
208
|
+
}))
|
|
209
|
+
});
|
|
210
|
+
if (p2.isCancel(selected)) {
|
|
211
|
+
p2.cancel("Operation cancelled");
|
|
212
|
+
process.exit(0);
|
|
213
|
+
}
|
|
214
|
+
templateId = selected;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
const template = templates_default.find(
|
|
218
|
+
(t) => t.id === templateId
|
|
219
|
+
);
|
|
220
|
+
p2.note(`Template: ${template.label}
|
|
221
|
+
App name: ${appName}`);
|
|
222
|
+
await createApp({ template, appName });
|
|
223
|
+
} catch (error) {
|
|
224
|
+
if (error instanceof Error) {
|
|
225
|
+
p2.log.error(`Error: ${error.message}`);
|
|
226
|
+
} else {
|
|
227
|
+
p2.log.error(`Unknown error: ${String(error)}`);
|
|
228
|
+
}
|
|
229
|
+
process.exit(1);
|
|
230
|
+
}
|
|
231
|
+
});
|
|
232
|
+
program.showHelpAfterError();
|
|
233
|
+
program.showSuggestionAfterError(true);
|
|
234
|
+
try {
|
|
235
|
+
await program.parseAsync(process.argv);
|
|
236
|
+
} catch (error) {
|
|
237
|
+
if (error instanceof Error) {
|
|
238
|
+
console.error(chalk.red(`Error: ${error.message}`));
|
|
239
|
+
process.exit(1);
|
|
240
|
+
}
|
|
241
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@drupal-canvas/create",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "CLI to scaffold a codebase for working with Drupal Canvas Code Components",
|
|
5
|
+
"license": "GPL-2.0-or-later",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://git.drupalcode.org/project/canvas.git"
|
|
9
|
+
},
|
|
10
|
+
"bugs": "https://www.drupal.org/project/issues/canvas",
|
|
11
|
+
"main": "dist/index.js",
|
|
12
|
+
"bin": "./dist/index.js",
|
|
13
|
+
"type": "module",
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"templates.json",
|
|
17
|
+
"README.md"
|
|
18
|
+
],
|
|
19
|
+
"keywords": [
|
|
20
|
+
"canvas",
|
|
21
|
+
"drupal",
|
|
22
|
+
"cli",
|
|
23
|
+
"create"
|
|
24
|
+
],
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": ">=18.20.0"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@clack/prompts": "^0.11.0",
|
|
30
|
+
"chalk": "^5.6.2",
|
|
31
|
+
"commander": "^14.0.2",
|
|
32
|
+
"cross-spawn": "^7.0.6",
|
|
33
|
+
"rimraf": "^6.1.0",
|
|
34
|
+
"simple-git": "^3.30.0",
|
|
35
|
+
"validate-npm-package-name": "^7.0.0"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@types/cross-spawn": "^6.0.6",
|
|
39
|
+
"@types/validate-npm-package-name": "^4.0.2",
|
|
40
|
+
"tsup": "^8.5.1",
|
|
41
|
+
"typescript": "^5.9.3"
|
|
42
|
+
},
|
|
43
|
+
"scripts": {
|
|
44
|
+
"start": "node dist/index.js",
|
|
45
|
+
"build": "NODE_ENV=production tsup",
|
|
46
|
+
"dev": "NODE_ENV=development tsup --watch",
|
|
47
|
+
"type-check": "tsc --noEmit --pretty"
|
|
48
|
+
}
|
|
49
|
+
}
|