slicejs-cli 4.3.0 → 4.5.0
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/LICENSE +21 -21
- package/README.md +260 -257
- package/client.js +958 -823
- package/commands/Print.js +163 -163
- package/commands/Validations.js +92 -92
- package/commands/build/build.js +97 -107
- package/commands/buildProduction/buildProduction.js +613 -613
- package/commands/bundle/bundle.js +251 -239
- package/commands/createComponent/VisualComponentTemplate.js +55 -55
- package/commands/createComponent/createComponent.js +128 -128
- package/commands/deleteComponent/deleteComponent.js +81 -81
- package/commands/docs/docs.js +365 -365
- package/commands/doctor/doctor.js +730 -563
- package/commands/getComponent/getComponent.js +841 -842
- package/commands/init/init.js +526 -475
- package/commands/lint/lint.js +75 -0
- package/commands/lint/types.js +85 -0
- package/commands/listComponents/listComponents.js +172 -172
- package/commands/seo/check.js +94 -0
- package/commands/seo/images.js +641 -0
- package/commands/seo/sitemap.js +109 -0
- package/commands/seo/support.js +192 -0
- package/commands/setup/lint.js +151 -0
- package/commands/skill/skill.js +236 -236
- package/commands/startServer/startServer.js +240 -240
- package/commands/startServer/watchServer.js +66 -66
- package/commands/translator/check.js +32 -0
- package/commands/translator/init.js +125 -0
- package/commands/translator/scan.js +444 -0
- package/commands/typecheck/typecheck.js +83 -0
- package/commands/types/types.js +1378 -1097
- package/commands/utils/JsMinifier.js +230 -230
- package/commands/utils/LocalCliDelegation.js +78 -53
- package/commands/utils/PackageManager.js +148 -148
- package/commands/utils/PathHelper.js +87 -75
- package/commands/utils/ProjectValidation.js +274 -0
- package/commands/utils/SourceDiagnostics.js +201 -201
- package/commands/utils/VersionChecker.js +169 -169
- package/commands/utils/analyzeSource.js +77 -77
- package/commands/utils/bundling/BundleGenerator.js +3748 -3776
- package/commands/utils/bundling/DependencyAnalyzer.js +12 -2
- package/commands/utils/bundling/ExternalModuleBundler.js +298 -298
- package/commands/utils/componentAudit.js +74 -0
- package/commands/utils/compressAssets.js +103 -103
- package/commands/utils/cssScope.js +83 -0
- package/commands/utils/loadConfig.js +31 -31
- package/commands/utils/semver.js +66 -0
- package/commands/utils/sliceConfigSchema.js +106 -0
- package/commands/utils/sliceScripts.js +48 -48
- package/commands/utils/updateManager.js +471 -471
- package/eslint-config.js +128 -0
- package/package.json +19 -13
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
// slice seo sitemap — write public/sitemap.xml (and robots.txt when the
|
|
2
|
+
// manifest asks for it) from seo.json.
|
|
3
|
+
//
|
|
4
|
+
// Both files are build artifacts a human commits: the command warns and asks
|
|
5
|
+
// before changing an existing file, `--yes` skips the question, and a
|
|
6
|
+
// non-interactive run fails instead of hanging.
|
|
7
|
+
|
|
8
|
+
import path from 'node:path';
|
|
9
|
+
import Print from '../Print.js';
|
|
10
|
+
import {
|
|
11
|
+
MANIFEST_FILE,
|
|
12
|
+
ROBOTS_FILE,
|
|
13
|
+
SITEMAP_FILE,
|
|
14
|
+
buildRobotsTxt,
|
|
15
|
+
loadFrameworkSeo,
|
|
16
|
+
loadManifest,
|
|
17
|
+
loadSeoTranslator,
|
|
18
|
+
writeFileConfirmed
|
|
19
|
+
} from './support.js';
|
|
20
|
+
|
|
21
|
+
const LOCAL_ORIGIN = /localhost|127\.0\.0\.1|0\.0\.0\.0|\.local\b/i;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Pure-ish core (everything injectable) so the tests do not need a real CLI.
|
|
25
|
+
* @returns {Promise<{ errors: string[], warnings: string[], files: Array<{file: string, status: string}> }>}
|
|
26
|
+
*/
|
|
27
|
+
export async function generateSeoFiles(options = {}) {
|
|
28
|
+
const { projectRoot, yes = false, dryRun = false, confirm = null } = options;
|
|
29
|
+
const framework = options.framework || await loadFrameworkSeo(projectRoot);
|
|
30
|
+
const manifest = options.manifest || loadManifest(projectRoot).manifest;
|
|
31
|
+
|
|
32
|
+
if (!manifest) {
|
|
33
|
+
return { errors: [`${MANIFEST_FILE} not found in ${projectRoot}`], warnings: [], files: [] };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const { errors, warnings } = framework.validateSeoManifest(manifest, {
|
|
37
|
+
publicDir: path.join(projectRoot, 'src', 'public'),
|
|
38
|
+
translator: await loadSeoTranslator(projectRoot, framework)
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// A sitemap written with localhost URLs is a deploy bug, not a warning.
|
|
42
|
+
if (framework.normalizeOrigin(manifest.origin) && LOCAL_ORIGIN.test(manifest.origin)) {
|
|
43
|
+
errors.push('origin points at localhost: refusing to write a sitemap with local URLs');
|
|
44
|
+
}
|
|
45
|
+
if (errors.length) return { errors, warnings, files: [] };
|
|
46
|
+
|
|
47
|
+
const origin = framework.normalizeOrigin(manifest.origin);
|
|
48
|
+
const translator = await loadSeoTranslator(projectRoot, framework);
|
|
49
|
+
const files = [];
|
|
50
|
+
|
|
51
|
+
files.push(await writeFileConfirmed(
|
|
52
|
+
path.join(projectRoot, SITEMAP_FILE),
|
|
53
|
+
framework.buildSitemap({ manifest, origin, translator }),
|
|
54
|
+
{ yes, dryRun, confirm }
|
|
55
|
+
));
|
|
56
|
+
|
|
57
|
+
const robots = buildRobotsTxt(manifest, origin);
|
|
58
|
+
if (robots) {
|
|
59
|
+
files.push(await writeFileConfirmed(
|
|
60
|
+
path.join(projectRoot, ROBOTS_FILE),
|
|
61
|
+
robots,
|
|
62
|
+
{ yes, dryRun, confirm }
|
|
63
|
+
));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return { errors, warnings, files };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const STATUS_LABEL = {
|
|
70
|
+
created: 'created',
|
|
71
|
+
updated: 'updated',
|
|
72
|
+
unchanged: 'unchanged (not rewritten)',
|
|
73
|
+
skipped: 'skipped (you said no)',
|
|
74
|
+
'would-change': 'would change',
|
|
75
|
+
'needs-confirmation': 'needs confirmation (re-run with --yes)'
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
export default async function sitemapCommand(options = {}, projectRoot) {
|
|
79
|
+
try {
|
|
80
|
+
const result = await generateSeoFiles({ projectRoot, ...options });
|
|
81
|
+
|
|
82
|
+
for (const warning of result.warnings) Print.warning(warning);
|
|
83
|
+
if (result.errors.length) {
|
|
84
|
+
for (const error of result.errors) Print.error(error);
|
|
85
|
+
process.exitCode = 1;
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
for (const entry of result.files) {
|
|
90
|
+
const label = STATUS_LABEL[entry.status] || entry.status;
|
|
91
|
+
const message = `${path.relative(projectRoot, entry.file)}: ${label}`;
|
|
92
|
+
if (entry.status === 'created' || entry.status === 'updated') Print.success(message);
|
|
93
|
+
else if (entry.status === 'unchanged') Print.info(message);
|
|
94
|
+
else if (entry.status === 'would-change') Print.info(message);
|
|
95
|
+
else {
|
|
96
|
+
Print.warning(message);
|
|
97
|
+
if (entry.status === 'needs-confirmation') process.exitCode = 1;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (result.files.some((entry) => entry.status === 'skipped')) {
|
|
102
|
+
Print.info('Nothing written. Re-run with --yes to overwrite.');
|
|
103
|
+
process.exitCode = 1;
|
|
104
|
+
}
|
|
105
|
+
} catch (error) {
|
|
106
|
+
Print.error('slice seo sitemap failed:', error.message);
|
|
107
|
+
process.exitCode = 1;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
// Shared plumbing for the `slice seo` commands: locating the project's
|
|
2
|
+
// framework, reading the manifest, and writing a file without ever clobbering
|
|
3
|
+
// one silently.
|
|
4
|
+
|
|
5
|
+
import fs from 'node:fs';
|
|
6
|
+
import fsp from 'node:fs/promises';
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
import { pathToFileURL } from 'node:url';
|
|
9
|
+
import { getProjectRoot } from '../utils/PathHelper.js';
|
|
10
|
+
|
|
11
|
+
export const MANIFEST_FILE = 'seo.json';
|
|
12
|
+
export const SITEMAP_FILE = path.join('src', 'public', 'sitemap.xml');
|
|
13
|
+
export const ROBOTS_FILE = path.join('src', 'public', 'robots.txt');
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The app's translator (config + dictionaries) as the framework's SEO context, or null
|
|
17
|
+
* for a single-language app / an older framework. `src` wins over `dist`: the CLI reads
|
|
18
|
+
* the authoring copy.
|
|
19
|
+
*/
|
|
20
|
+
export async function loadSeoTranslator(projectRoot, framework) {
|
|
21
|
+
let translator = null;
|
|
22
|
+
for (const folder of ['src', 'dist']) {
|
|
23
|
+
const file = path.join(projectRoot, folder, 'sliceConfig.json');
|
|
24
|
+
if (!fs.existsSync(file)) continue;
|
|
25
|
+
try {
|
|
26
|
+
translator = JSON.parse(fs.readFileSync(file, 'utf8')).translator || null;
|
|
27
|
+
} catch {
|
|
28
|
+
translator = null;
|
|
29
|
+
}
|
|
30
|
+
if (translator) break;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (translator?.enabled !== true || typeof framework?.buildTranslatorContext !== 'function') {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const folder = String(translator.path || '/locales').replace(/^\/+|\/+$/g, '') || 'locales';
|
|
38
|
+
const locales = [
|
|
39
|
+
translator.defaultLocale,
|
|
40
|
+
...(Array.isArray(translator.locales) ? translator.locales : []),
|
|
41
|
+
translator.fallbackLocale
|
|
42
|
+
].filter((value) => typeof value === 'string' && value);
|
|
43
|
+
|
|
44
|
+
const dictionaries = {};
|
|
45
|
+
for (const locale of [...new Set(locales)]) {
|
|
46
|
+
for (const base of ['src', 'dist']) {
|
|
47
|
+
const root = path.join(projectRoot, base, folder);
|
|
48
|
+
const file = path.join(root, `${locale}.json`);
|
|
49
|
+
const namespaceDir = path.join(root, locale);
|
|
50
|
+
if (!fs.existsSync(file) && !fs.existsSync(namespaceDir)) continue;
|
|
51
|
+
let messages = {};
|
|
52
|
+
if (fs.existsSync(file)) {
|
|
53
|
+
try {
|
|
54
|
+
messages = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
55
|
+
} catch (error) {
|
|
56
|
+
throw new Error(`invalid translator dictionary ${path.join(base, folder, `${locale}.json`)}: ${error.message}`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
if (fs.existsSync(namespaceDir) && fs.statSync(namespaceDir).isDirectory()) {
|
|
60
|
+
for (const name of fs.readdirSync(namespaceDir)) {
|
|
61
|
+
if (!name.endsWith('.json')) continue;
|
|
62
|
+
try {
|
|
63
|
+
messages[name.slice(0, -5)] = JSON.parse(fs.readFileSync(path.join(namespaceDir, name), 'utf8'));
|
|
64
|
+
} catch (error) {
|
|
65
|
+
throw new Error(`invalid translator dictionary ${path.join(base, folder, locale, name)}: ${error.message}`);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
dictionaries[locale] = messages;
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return framework.buildTranslatorContext(translator, dictionaries);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export const resolveRoot = (moduleUrl) => getProjectRoot(moduleUrl);
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* The validator and the builders live in the framework, so the rules cannot
|
|
81
|
+
* drift between `slice seo check`, `slice doctor` and the server. Resolve it
|
|
82
|
+
* from the project first: that is the version the app actually runs.
|
|
83
|
+
*/
|
|
84
|
+
export async function loadFrameworkSeo(projectRoot) {
|
|
85
|
+
const seoPath = path.join(projectRoot, 'node_modules', 'slicejs-web-framework', 'api', 'framework', 'seo.js');
|
|
86
|
+
if (fs.existsSync(seoPath)) {
|
|
87
|
+
return import(pathToFileURL(seoPath).href);
|
|
88
|
+
}
|
|
89
|
+
try {
|
|
90
|
+
return await import('slicejs-web-framework/api/framework/seo.js');
|
|
91
|
+
} catch (error) {
|
|
92
|
+
throw new Error(
|
|
93
|
+
`Cannot load the framework SEO module from ${projectRoot} ` +
|
|
94
|
+
`(${error.message}). Update slicejs-web-framework to a version that ships api/framework/seo.js.`,
|
|
95
|
+
{ cause: error }
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Find the manifest. Apps that hand-write it keep it at the project root; a
|
|
102
|
+
* generated pipeline writes it inside the deployed public/ folder, which is
|
|
103
|
+
* where the framework server looks too.
|
|
104
|
+
*/
|
|
105
|
+
export function loadManifest(projectRoot) {
|
|
106
|
+
const candidates = [
|
|
107
|
+
path.join(projectRoot, MANIFEST_FILE),
|
|
108
|
+
path.join(projectRoot, 'src', 'public', MANIFEST_FILE),
|
|
109
|
+
path.join(projectRoot, 'dist', 'public', MANIFEST_FILE)
|
|
110
|
+
];
|
|
111
|
+
const file = candidates.find((candidate) => fs.existsSync(candidate));
|
|
112
|
+
if (!file) return { file: candidates[0], manifest: null };
|
|
113
|
+
return { file, manifest: JSON.parse(fs.readFileSync(file, 'utf8')) };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Prompt only when there is a human: CI must fail, never hang. */
|
|
117
|
+
export async function promptYesNo(question, { defaultValue = false } = {}) {
|
|
118
|
+
const { createInterface } = await import('node:readline/promises');
|
|
119
|
+
const rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
120
|
+
try {
|
|
121
|
+
const suffix = defaultValue ? '[Y/n]' : '[y/N]';
|
|
122
|
+
const answer = (await rl.question(`${question} ${suffix} `)).trim().toLowerCase();
|
|
123
|
+
if (!answer) return defaultValue;
|
|
124
|
+
return answer === 'y' || answer === 'yes';
|
|
125
|
+
} finally {
|
|
126
|
+
rl.close();
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const isInteractive = () => Boolean(process.stdin.isTTY && process.stdout.isTTY);
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Write `content` to `file` unless that would clobber a different file without
|
|
134
|
+
* permission.
|
|
135
|
+
*
|
|
136
|
+
* Statuses: `created`, `unchanged` (nothing written, no mtime churn),
|
|
137
|
+
* `updated`, `skipped` (the user said no), `would-change` (dry run),
|
|
138
|
+
* `needs-confirmation` (no TTY and no --yes).
|
|
139
|
+
*
|
|
140
|
+
* @returns {Promise<{ status: string, file: string }>}
|
|
141
|
+
*/
|
|
142
|
+
export async function writeFileConfirmed(file, content, options = {}) {
|
|
143
|
+
const { yes = false, dryRun = false, confirm = null } = options;
|
|
144
|
+
|
|
145
|
+
const exists = fs.existsSync(file);
|
|
146
|
+
const current = exists ? fs.readFileSync(file, 'utf8') : null;
|
|
147
|
+
if (current === content) return { status: 'unchanged', file };
|
|
148
|
+
if (!exists) {
|
|
149
|
+
if (!dryRun) await writeAtomic(file, content);
|
|
150
|
+
return { status: dryRun ? 'would-change' : 'created', file };
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (dryRun) return { status: 'would-change', file };
|
|
154
|
+
if (!yes) {
|
|
155
|
+
const allowed = confirm
|
|
156
|
+
? await confirm(file)
|
|
157
|
+
: isInteractive()
|
|
158
|
+
? await promptYesNo(`${file} already exists and would change. Overwrite it?`, { defaultValue: false })
|
|
159
|
+
: false;
|
|
160
|
+
if (!allowed) {
|
|
161
|
+
return { status: isInteractive() || confirm ? 'skipped' : 'needs-confirmation', file };
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
await writeAtomic(file, content);
|
|
166
|
+
return { status: 'updated', file };
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
async function writeAtomic(file, content) {
|
|
170
|
+
await fsp.mkdir(path.dirname(file), { recursive: true });
|
|
171
|
+
const tmp = `${file}.${process.pid}.tmp`;
|
|
172
|
+
await fsp.writeFile(tmp, content);
|
|
173
|
+
await fsp.rename(tmp, file);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* `robots.txt` from the manifest. Returns null when the manifest does not ask
|
|
178
|
+
* for one, so a hand-written file is never touched.
|
|
179
|
+
*/
|
|
180
|
+
export function buildRobotsTxt(manifest, origin) {
|
|
181
|
+
const config = manifest?.robotsTxt;
|
|
182
|
+
if (!config || config === false) return null;
|
|
183
|
+
|
|
184
|
+
const lines = ['User-agent: *'];
|
|
185
|
+
const allow = Array.isArray(config.allow) ? config.allow : [];
|
|
186
|
+
const disallow = Array.isArray(config.disallow) ? config.disallow : [];
|
|
187
|
+
if (!allow.length && !disallow.length) lines.push('Allow: /');
|
|
188
|
+
for (const entry of allow) lines.push(`Allow: ${entry}`);
|
|
189
|
+
for (const entry of disallow) lines.push(`Disallow: ${entry}`);
|
|
190
|
+
if (config.sitemap !== false) lines.push(`Sitemap: ${origin}/sitemap.xml`);
|
|
191
|
+
return `${lines.join('\n')}\n`;
|
|
192
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
// slice setup lint — add linting to a project that already exists.
|
|
2
|
+
//
|
|
3
|
+
// `slice init` does not decide this for you: ESLint is a choice (some projects
|
|
4
|
+
// use another linter, some none), and init only runs once. This command is
|
|
5
|
+
// idempotent, never overwrites an eslint config you wrote, and works on any
|
|
6
|
+
// Slice project.
|
|
7
|
+
|
|
8
|
+
import fs from 'fs-extra';
|
|
9
|
+
import path from 'node:path';
|
|
10
|
+
import { execSync } from 'node:child_process';
|
|
11
|
+
import Print from '../Print.js';
|
|
12
|
+
import { installCommand, resolvePackageManager } from '../utils/PackageManager.js';
|
|
13
|
+
import { runGenerateTypes } from '../types/types.js';
|
|
14
|
+
|
|
15
|
+
export const ESLINT_CONFIG_FILE = 'eslint.config.js';
|
|
16
|
+
|
|
17
|
+
/** The config written when the project has none. */
|
|
18
|
+
export const eslintConfigContent = `import slice from 'slicejs-cli/eslint-config.js';
|
|
19
|
+
|
|
20
|
+
export default [...slice];
|
|
21
|
+
`;
|
|
22
|
+
|
|
23
|
+
const LINT_SCRIPTS = {
|
|
24
|
+
lint: 'slice lint',
|
|
25
|
+
'lint:fix': 'slice lint --fix'
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const TYPE_SCRIPTS = {
|
|
29
|
+
// Same command init writes, so a project set up later has the script too.
|
|
30
|
+
'slice:types': 'node ./node_modules/slicejs-cli/client.js types generate',
|
|
31
|
+
typecheck: 'slice typecheck'
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const hasLocalPackage = (projectRoot, name) => fs.pathExistsSync(
|
|
35
|
+
path.join(projectRoot, 'node_modules', name, 'package.json')
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Inspects, installs and writes. `runInstall` is injectable so the tests do not
|
|
40
|
+
* touch the network.
|
|
41
|
+
*/
|
|
42
|
+
export async function setupLint({ projectRoot, dryRun = false, withTypes = false, runInstall = null }) {
|
|
43
|
+
const packagePath = path.join(projectRoot, 'package.json');
|
|
44
|
+
if (!(await fs.pathExists(packagePath))) {
|
|
45
|
+
return {
|
|
46
|
+
ok: false,
|
|
47
|
+
error: 'No package.json here. Run this from the root of a Slice project.',
|
|
48
|
+
actions: []
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const pkg = await fs.readJson(packagePath);
|
|
53
|
+
const packageManager = resolvePackageManager(projectRoot).name;
|
|
54
|
+
const actions = [];
|
|
55
|
+
|
|
56
|
+
// 1. The tools the config and the command need, as devDependencies.
|
|
57
|
+
const required = withTypes ? ['eslint', 'slicejs-cli', 'typescript'] : ['eslint', 'slicejs-cli'];
|
|
58
|
+
const missing = required.filter((name) => !hasLocalPackage(projectRoot, name));
|
|
59
|
+
if (missing.length > 0) {
|
|
60
|
+
const command = installCommand(packageManager, missing.join(' '), { dev: true });
|
|
61
|
+
actions.push({ kind: 'install', detail: command });
|
|
62
|
+
if (!dryRun) {
|
|
63
|
+
if (runInstall) await runInstall(command);
|
|
64
|
+
else execSync(command, { cwd: projectRoot, stdio: 'inherit' });
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// 2. The config, only when there is none. A project may have its own, and it
|
|
69
|
+
// may use another linter entirely: never overwrite it.
|
|
70
|
+
const configPath = path.join(projectRoot, ESLINT_CONFIG_FILE);
|
|
71
|
+
const hasConfig = await fs.pathExists(configPath);
|
|
72
|
+
if (!hasConfig) {
|
|
73
|
+
actions.push({ kind: 'write', detail: ESLINT_CONFIG_FILE });
|
|
74
|
+
if (!dryRun) await fs.writeFile(configPath, eslintConfigContent, 'utf8');
|
|
75
|
+
} else {
|
|
76
|
+
actions.push({ kind: 'keep', detail: `${ESLINT_CONFIG_FILE} already exists` });
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// 3. Scripts, without clobbering what the project already wires.
|
|
80
|
+
const scripts = { ...(pkg.scripts || {}) };
|
|
81
|
+
const scriptChanges = [];
|
|
82
|
+
const wanted = withTypes ? { ...LINT_SCRIPTS, ...TYPE_SCRIPTS } : LINT_SCRIPTS;
|
|
83
|
+
for (const [name, command] of Object.entries(wanted)) {
|
|
84
|
+
if (scripts[name] === undefined) {
|
|
85
|
+
scripts[name] = command;
|
|
86
|
+
scriptChanges.push({ name, command });
|
|
87
|
+
} else if (scripts[name] !== command) {
|
|
88
|
+
actions.push({ kind: 'keep', detail: `script "${name}" already set to "${scripts[name]}"` });
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (scriptChanges.length > 0) {
|
|
93
|
+
actions.push({ kind: 'scripts', detail: scriptChanges.map((change) => `${change.name}: ${change.command}`).join(', ') });
|
|
94
|
+
if (!dryRun) {
|
|
95
|
+
await fs.writeJson(packagePath, { ...pkg, scripts }, { spaces: 2 });
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// With TypeScript installed, generate the declarations and the jsconfig so
|
|
100
|
+
// `slice typecheck` works immediately. Only when the project has components.
|
|
101
|
+
if (withTypes && !dryRun && await fs.pathExists(path.join(projectRoot, 'src', 'Components', 'components.js'))) {
|
|
102
|
+
try {
|
|
103
|
+
await runGenerateTypes({
|
|
104
|
+
projectRoot,
|
|
105
|
+
outputPath: path.join(projectRoot, 'src', 'slice-build.generated.d.ts')
|
|
106
|
+
});
|
|
107
|
+
actions.push({ kind: 'types', detail: 'src/slice-build.generated.d.ts + jsconfig.json' });
|
|
108
|
+
} catch (error) {
|
|
109
|
+
actions.push({ kind: 'types-failed', detail: error.message });
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return { ok: true, actions, dryRun };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export default async function setupLintCommand(options = {}, projectRoot) {
|
|
117
|
+
const dryRun = Boolean(options.dryRun);
|
|
118
|
+
|
|
119
|
+
let result;
|
|
120
|
+
try {
|
|
121
|
+
result = await setupLint({ projectRoot, dryRun, withTypes: Boolean(options.withTypes) });
|
|
122
|
+
} catch (error) {
|
|
123
|
+
Print.error('slice setup lint:', error.message);
|
|
124
|
+
process.exitCode = 1;
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (!result.ok) {
|
|
129
|
+
Print.error(result.error);
|
|
130
|
+
process.exitCode = 1;
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
for (const action of result.actions) {
|
|
135
|
+
if (action.kind === 'install') Print.info(`Install devDependencies: ${action.detail}`);
|
|
136
|
+
else if (action.kind === 'write') Print.success(`Created ${action.detail}`);
|
|
137
|
+
else if (action.kind === 'scripts') Print.success(`Added scripts: ${action.detail}`);
|
|
138
|
+
else if (action.kind === 'types') Print.success(`Generated declarations: ${action.detail}`);
|
|
139
|
+
else if (action.kind === 'types-failed') Print.warning(`Could not generate the declarations: ${action.detail}`);
|
|
140
|
+
else Print.info(action.detail);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (dryRun) {
|
|
144
|
+
Print.info('Dry run: nothing was written.');
|
|
145
|
+
return true;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
Print.newLine();
|
|
149
|
+
Print.success('Linting is set up. Run "slice lint" (or "npm run lint").');
|
|
150
|
+
return true;
|
|
151
|
+
}
|