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
|
@@ -1,169 +1,169 @@
|
|
|
1
|
-
// commands/utils/VersionChecker.js
|
|
2
|
-
|
|
3
|
-
import fs from "fs-extra";
|
|
4
|
-
import path from "path";
|
|
5
|
-
import { fileURLToPath } from "url";
|
|
6
|
-
import Print from "../Print.js";
|
|
7
|
-
import { getProjectRoot, getPath } from "../utils/PathHelper.js";
|
|
8
|
-
import { resolvePackageManager } from "../utils/PackageManager.js";
|
|
9
|
-
|
|
10
|
-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
11
|
-
|
|
12
|
-
class VersionChecker {
|
|
13
|
-
constructor() {
|
|
14
|
-
this.currentCliVersion = null;
|
|
15
|
-
this.currentFrameworkVersion = null;
|
|
16
|
-
this.latestCliVersion = null;
|
|
17
|
-
this.latestFrameworkVersion = null;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
async getCurrentVersions() {
|
|
21
|
-
try {
|
|
22
|
-
// Get CLI version
|
|
23
|
-
const cliPackagePath = path.join(__dirname, '../../package.json');
|
|
24
|
-
const cliPackage = await fs.readJson(cliPackagePath);
|
|
25
|
-
this.currentCliVersion = cliPackage.version;
|
|
26
|
-
|
|
27
|
-
// Get Framework version from project node_modules
|
|
28
|
-
const projectRoot = getProjectRoot(import.meta.url);
|
|
29
|
-
const frameworkPackagePath = getPath(import.meta.url, 'node_modules', 'slicejs-web-framework', 'package.json');
|
|
30
|
-
if (await fs.pathExists(frameworkPackagePath)) {
|
|
31
|
-
const frameworkPackage = await fs.readJson(frameworkPackagePath);
|
|
32
|
-
this.currentFrameworkVersion = frameworkPackage.version;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// Get Project's CLI version
|
|
36
|
-
const projectPackagePath = getPath(import.meta.url, 'package.json');
|
|
37
|
-
if (await fs.pathExists(projectPackagePath)) {
|
|
38
|
-
const projectPackage = await fs.readJson(projectPackagePath);
|
|
39
|
-
if (projectPackage.dependencies && projectPackage.dependencies['slicejs-cli']) {
|
|
40
|
-
// This could be different from the currently running CLI version
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
return {
|
|
45
|
-
cli: this.currentCliVersion,
|
|
46
|
-
framework: this.currentFrameworkVersion
|
|
47
|
-
};
|
|
48
|
-
} catch (error) {
|
|
49
|
-
return null;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
async getLatestVersions() {
|
|
54
|
-
try {
|
|
55
|
-
// Check CLI version
|
|
56
|
-
const cliResponse = await fetch('https://registry.npmjs.org/slicejs-cli/latest', {
|
|
57
|
-
headers: { 'Accept': 'application/json' }
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
if (cliResponse.ok) {
|
|
61
|
-
const cliData = await cliResponse.json();
|
|
62
|
-
this.latestCliVersion = cliData.version;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
// Check Framework version
|
|
66
|
-
const frameworkResponse = await fetch('https://registry.npmjs.org/slicejs-web-framework/latest', {
|
|
67
|
-
headers: { 'Accept': 'application/json' }
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
if (frameworkResponse.ok) {
|
|
71
|
-
const frameworkData = await frameworkResponse.json();
|
|
72
|
-
this.latestFrameworkVersion = frameworkData.version;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
return {
|
|
76
|
-
cli: this.latestCliVersion,
|
|
77
|
-
framework: this.latestFrameworkVersion
|
|
78
|
-
};
|
|
79
|
-
} catch (error) {
|
|
80
|
-
// Silent fail - don't interrupt commands for version check failures
|
|
81
|
-
return null;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
compareVersions(current, latest) {
|
|
86
|
-
if (!current || !latest) return null;
|
|
87
|
-
|
|
88
|
-
const currentParts = current.split('.').map(Number);
|
|
89
|
-
const latestParts = latest.split('.').map(Number);
|
|
90
|
-
|
|
91
|
-
for (let i = 0; i < Math.max(currentParts.length, latestParts.length); i++) {
|
|
92
|
-
const currentPart = currentParts[i] || 0;
|
|
93
|
-
const latestPart = latestParts[i] || 0;
|
|
94
|
-
|
|
95
|
-
if (latestPart > currentPart) return 'outdated';
|
|
96
|
-
if (currentPart > latestPart) return 'newer';
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
return 'current';
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
async checkForUpdates(silent = false) {
|
|
103
|
-
try {
|
|
104
|
-
const current = await this.getCurrentVersions();
|
|
105
|
-
if (!current) return;
|
|
106
|
-
|
|
107
|
-
const latest = await this.getLatestVersions();
|
|
108
|
-
if (!latest) return;
|
|
109
|
-
|
|
110
|
-
const cliStatus = this.compareVersions(current.cli, latest.cli);
|
|
111
|
-
const frameworkStatus = this.compareVersions(current.framework, latest.framework);
|
|
112
|
-
|
|
113
|
-
if (!silent && (cliStatus === 'outdated' || frameworkStatus === 'outdated')) {
|
|
114
|
-
const pm = resolvePackageManager(getProjectRoot(import.meta.url)).name;
|
|
115
|
-
console.log(''); // Line break
|
|
116
|
-
Print.warning('📦 Available Updates:');
|
|
117
|
-
|
|
118
|
-
if (cliStatus === 'outdated') {
|
|
119
|
-
console.log(` 🔧 CLI: ${current.cli} → ${latest.cli}`);
|
|
120
|
-
console.log(` ${pm} update slicejs-cli`);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
if (frameworkStatus === 'outdated') {
|
|
124
|
-
console.log(` ⚡ Framework: ${current.framework} → ${latest.framework}`);
|
|
125
|
-
console.log(` ${pm} update slicejs-web-framework`);
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
console.log(' 📚 Changelog: https://github.com/VKneider/slice.js/releases');
|
|
129
|
-
console.log(''); // Line break
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
return {
|
|
133
|
-
cli: { current: current.cli, latest: latest.cli, status: cliStatus },
|
|
134
|
-
framework: { current: current.framework, latest: latest.framework, status: frameworkStatus }
|
|
135
|
-
};
|
|
136
|
-
|
|
137
|
-
} catch (error) {
|
|
138
|
-
// Silent fail - don't interrupt commands
|
|
139
|
-
return null;
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
async showVersionInfo() {
|
|
144
|
-
const current = await this.getCurrentVersions();
|
|
145
|
-
const latest = await this.getLatestVersions();
|
|
146
|
-
|
|
147
|
-
console.log('\n📋 Version Information:');
|
|
148
|
-
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
|
|
149
|
-
|
|
150
|
-
if (current?.cli) {
|
|
151
|
-
const cliStatus = this.compareVersions(current.cli, latest?.cli);
|
|
152
|
-
const statusIcon = cliStatus === 'current' ? '✅' : cliStatus === 'outdated' ? '🔄' : '🆕';
|
|
153
|
-
console.log(`${statusIcon} CLI: v${current.cli}${latest?.cli && latest.cli !== current.cli ? ` (latest: v${latest.cli})` : ''}`);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
if (current?.framework) {
|
|
157
|
-
const frameworkStatus = this.compareVersions(current.framework, latest?.framework);
|
|
158
|
-
const statusIcon = frameworkStatus === 'current' ? '✅' : frameworkStatus === 'outdated' ? '🔄' : '🆕';
|
|
159
|
-
console.log(`${statusIcon} Framework: v${current.framework}${latest?.framework && latest.framework !== current.framework ? ` (latest: v${latest.framework})` : ''}`);
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n');
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
// Singleton instance
|
|
167
|
-
const versionChecker = new VersionChecker();
|
|
168
|
-
|
|
169
|
-
export default versionChecker;
|
|
1
|
+
// commands/utils/VersionChecker.js
|
|
2
|
+
|
|
3
|
+
import fs from "fs-extra";
|
|
4
|
+
import path from "path";
|
|
5
|
+
import { fileURLToPath } from "url";
|
|
6
|
+
import Print from "../Print.js";
|
|
7
|
+
import { getProjectRoot, getPath } from "../utils/PathHelper.js";
|
|
8
|
+
import { resolvePackageManager } from "../utils/PackageManager.js";
|
|
9
|
+
|
|
10
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
11
|
+
|
|
12
|
+
class VersionChecker {
|
|
13
|
+
constructor() {
|
|
14
|
+
this.currentCliVersion = null;
|
|
15
|
+
this.currentFrameworkVersion = null;
|
|
16
|
+
this.latestCliVersion = null;
|
|
17
|
+
this.latestFrameworkVersion = null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async getCurrentVersions() {
|
|
21
|
+
try {
|
|
22
|
+
// Get CLI version
|
|
23
|
+
const cliPackagePath = path.join(__dirname, '../../package.json');
|
|
24
|
+
const cliPackage = await fs.readJson(cliPackagePath);
|
|
25
|
+
this.currentCliVersion = cliPackage.version;
|
|
26
|
+
|
|
27
|
+
// Get Framework version from project node_modules
|
|
28
|
+
const projectRoot = getProjectRoot(import.meta.url);
|
|
29
|
+
const frameworkPackagePath = getPath(import.meta.url, 'node_modules', 'slicejs-web-framework', 'package.json');
|
|
30
|
+
if (await fs.pathExists(frameworkPackagePath)) {
|
|
31
|
+
const frameworkPackage = await fs.readJson(frameworkPackagePath);
|
|
32
|
+
this.currentFrameworkVersion = frameworkPackage.version;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Get Project's CLI version
|
|
36
|
+
const projectPackagePath = getPath(import.meta.url, 'package.json');
|
|
37
|
+
if (await fs.pathExists(projectPackagePath)) {
|
|
38
|
+
const projectPackage = await fs.readJson(projectPackagePath);
|
|
39
|
+
if (projectPackage.dependencies && projectPackage.dependencies['slicejs-cli']) {
|
|
40
|
+
// This could be different from the currently running CLI version
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return {
|
|
45
|
+
cli: this.currentCliVersion,
|
|
46
|
+
framework: this.currentFrameworkVersion
|
|
47
|
+
};
|
|
48
|
+
} catch (error) {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async getLatestVersions() {
|
|
54
|
+
try {
|
|
55
|
+
// Check CLI version
|
|
56
|
+
const cliResponse = await fetch('https://registry.npmjs.org/slicejs-cli/latest', {
|
|
57
|
+
headers: { 'Accept': 'application/json' }
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
if (cliResponse.ok) {
|
|
61
|
+
const cliData = await cliResponse.json();
|
|
62
|
+
this.latestCliVersion = cliData.version;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Check Framework version
|
|
66
|
+
const frameworkResponse = await fetch('https://registry.npmjs.org/slicejs-web-framework/latest', {
|
|
67
|
+
headers: { 'Accept': 'application/json' }
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
if (frameworkResponse.ok) {
|
|
71
|
+
const frameworkData = await frameworkResponse.json();
|
|
72
|
+
this.latestFrameworkVersion = frameworkData.version;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return {
|
|
76
|
+
cli: this.latestCliVersion,
|
|
77
|
+
framework: this.latestFrameworkVersion
|
|
78
|
+
};
|
|
79
|
+
} catch (error) {
|
|
80
|
+
// Silent fail - don't interrupt commands for version check failures
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
compareVersions(current, latest) {
|
|
86
|
+
if (!current || !latest) return null;
|
|
87
|
+
|
|
88
|
+
const currentParts = current.split('.').map(Number);
|
|
89
|
+
const latestParts = latest.split('.').map(Number);
|
|
90
|
+
|
|
91
|
+
for (let i = 0; i < Math.max(currentParts.length, latestParts.length); i++) {
|
|
92
|
+
const currentPart = currentParts[i] || 0;
|
|
93
|
+
const latestPart = latestParts[i] || 0;
|
|
94
|
+
|
|
95
|
+
if (latestPart > currentPart) return 'outdated';
|
|
96
|
+
if (currentPart > latestPart) return 'newer';
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return 'current';
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async checkForUpdates(silent = false) {
|
|
103
|
+
try {
|
|
104
|
+
const current = await this.getCurrentVersions();
|
|
105
|
+
if (!current) return;
|
|
106
|
+
|
|
107
|
+
const latest = await this.getLatestVersions();
|
|
108
|
+
if (!latest) return;
|
|
109
|
+
|
|
110
|
+
const cliStatus = this.compareVersions(current.cli, latest.cli);
|
|
111
|
+
const frameworkStatus = this.compareVersions(current.framework, latest.framework);
|
|
112
|
+
|
|
113
|
+
if (!silent && (cliStatus === 'outdated' || frameworkStatus === 'outdated')) {
|
|
114
|
+
const pm = resolvePackageManager(getProjectRoot(import.meta.url)).name;
|
|
115
|
+
console.log(''); // Line break
|
|
116
|
+
Print.warning('📦 Available Updates:');
|
|
117
|
+
|
|
118
|
+
if (cliStatus === 'outdated') {
|
|
119
|
+
console.log(` 🔧 CLI: ${current.cli} → ${latest.cli}`);
|
|
120
|
+
console.log(` ${pm} update slicejs-cli`);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (frameworkStatus === 'outdated') {
|
|
124
|
+
console.log(` ⚡ Framework: ${current.framework} → ${latest.framework}`);
|
|
125
|
+
console.log(` ${pm} update slicejs-web-framework`);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
console.log(' 📚 Changelog: https://github.com/VKneider/slice.js/releases');
|
|
129
|
+
console.log(''); // Line break
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return {
|
|
133
|
+
cli: { current: current.cli, latest: latest.cli, status: cliStatus },
|
|
134
|
+
framework: { current: current.framework, latest: latest.framework, status: frameworkStatus }
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
} catch (error) {
|
|
138
|
+
// Silent fail - don't interrupt commands
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
async showVersionInfo() {
|
|
144
|
+
const current = await this.getCurrentVersions();
|
|
145
|
+
const latest = await this.getLatestVersions();
|
|
146
|
+
|
|
147
|
+
console.log('\n📋 Version Information:');
|
|
148
|
+
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
|
|
149
|
+
|
|
150
|
+
if (current?.cli) {
|
|
151
|
+
const cliStatus = this.compareVersions(current.cli, latest?.cli);
|
|
152
|
+
const statusIcon = cliStatus === 'current' ? '✅' : cliStatus === 'outdated' ? '🔄' : '🆕';
|
|
153
|
+
console.log(`${statusIcon} CLI: v${current.cli}${latest?.cli && latest.cli !== current.cli ? ` (latest: v${latest.cli})` : ''}`);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (current?.framework) {
|
|
157
|
+
const frameworkStatus = this.compareVersions(current.framework, latest?.framework);
|
|
158
|
+
const statusIcon = frameworkStatus === 'current' ? '✅' : frameworkStatus === 'outdated' ? '🔄' : '🆕';
|
|
159
|
+
console.log(`${statusIcon} Framework: v${current.framework}${latest?.framework && latest.framework !== current.framework ? ` (latest: v${latest.framework})` : ''}`);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n');
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Singleton instance
|
|
167
|
+
const versionChecker = new VersionChecker();
|
|
168
|
+
|
|
169
|
+
export default versionChecker;
|
|
@@ -1,77 +1,77 @@
|
|
|
1
|
-
// commands/utils/analyzeSource.js
|
|
2
|
-
//
|
|
3
|
-
// Static dependency extraction for a single component/module source file.
|
|
4
|
-
// Used by the `get` flow to discover what a downloaded component needs so the
|
|
5
|
-
// whole dependency tree is installed, not just its own three files.
|
|
6
|
-
//
|
|
7
|
-
// Two kinds of dependency are reported:
|
|
8
|
-
// - builds: component names referenced via slice.build('Name') → components
|
|
9
|
-
// - imports: relative import/export specifiers (start with '.') → components OR helper modules
|
|
10
|
-
//
|
|
11
|
-
// Only static forms are detected (string-literal build names, static import
|
|
12
|
-
// specifiers). Dynamic `slice.build(variable)` cannot be resolved here.
|
|
13
|
-
|
|
14
|
-
import { parse } from '@babel/parser';
|
|
15
|
-
import traverse from '@babel/traverse';
|
|
16
|
-
|
|
17
|
-
/** True for bare node_modules specifiers (not relative/absolute/URL/subpath-#). */
|
|
18
|
-
function isBare(spec) {
|
|
19
|
-
return typeof spec === 'string' && spec.length > 0 &&
|
|
20
|
-
!spec.startsWith('.') && !spec.startsWith('/') && !spec.startsWith('#') &&
|
|
21
|
-
!/^[a-zA-Z][a-zA-Z0-9+.-]*:/.test(spec);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* @param {string} sourceText
|
|
26
|
-
* @returns {{ builds: Set<string>, imports: string[], bareImports: string[] }}
|
|
27
|
-
*/
|
|
28
|
-
export function analyzeSource(sourceText) {
|
|
29
|
-
const builds = new Set();
|
|
30
|
-
const imports = new Set();
|
|
31
|
-
const bareImports = new Set();
|
|
32
|
-
|
|
33
|
-
const addImport = (node) => {
|
|
34
|
-
const src = node?.source?.value;
|
|
35
|
-
if (typeof src !== 'string') return;
|
|
36
|
-
if (src.startsWith('.')) imports.add(src);
|
|
37
|
-
else if (isBare(src)) bareImports.add(src);
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
try {
|
|
41
|
-
const ast = parse(sourceText, { sourceType: 'module', plugins: ['jsx'] });
|
|
42
|
-
|
|
43
|
-
traverse.default(ast, {
|
|
44
|
-
CallExpression(path) {
|
|
45
|
-
const { callee, arguments: args } = path.node;
|
|
46
|
-
|
|
47
|
-
// slice.build('Name', ...)
|
|
48
|
-
if (
|
|
49
|
-
callee.type === 'MemberExpression' &&
|
|
50
|
-
callee.object?.name === 'slice' &&
|
|
51
|
-
callee.property?.name === 'build' &&
|
|
52
|
-
args[0]?.type === 'StringLiteral'
|
|
53
|
-
) {
|
|
54
|
-
builds.add(args[0].value);
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// dynamic import('./x.js') or import('some-pkg')
|
|
59
|
-
if (callee.type === 'Import' && args[0]?.type === 'StringLiteral') {
|
|
60
|
-
const spec = args[0].value;
|
|
61
|
-
if (spec.startsWith('.')) imports.add(spec);
|
|
62
|
-
else if (isBare(spec)) bareImports.add(spec);
|
|
63
|
-
}
|
|
64
|
-
},
|
|
65
|
-
|
|
66
|
-
ImportDeclaration(path) { addImport(path.node); },
|
|
67
|
-
ExportNamedDeclaration(path) { addImport(path.node); },
|
|
68
|
-
ExportAllDeclaration(path) { addImport(path.node); }
|
|
69
|
-
});
|
|
70
|
-
} catch {
|
|
71
|
-
// Unparseable source → discover nothing; the component still installs as-is.
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
return { builds, imports: Array.from(imports), bareImports: Array.from(bareImports) };
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
export default analyzeSource;
|
|
1
|
+
// commands/utils/analyzeSource.js
|
|
2
|
+
//
|
|
3
|
+
// Static dependency extraction for a single component/module source file.
|
|
4
|
+
// Used by the `get` flow to discover what a downloaded component needs so the
|
|
5
|
+
// whole dependency tree is installed, not just its own three files.
|
|
6
|
+
//
|
|
7
|
+
// Two kinds of dependency are reported:
|
|
8
|
+
// - builds: component names referenced via slice.build('Name') → components
|
|
9
|
+
// - imports: relative import/export specifiers (start with '.') → components OR helper modules
|
|
10
|
+
//
|
|
11
|
+
// Only static forms are detected (string-literal build names, static import
|
|
12
|
+
// specifiers). Dynamic `slice.build(variable)` cannot be resolved here.
|
|
13
|
+
|
|
14
|
+
import { parse } from '@babel/parser';
|
|
15
|
+
import traverse from '@babel/traverse';
|
|
16
|
+
|
|
17
|
+
/** True for bare node_modules specifiers (not relative/absolute/URL/subpath-#). */
|
|
18
|
+
function isBare(spec) {
|
|
19
|
+
return typeof spec === 'string' && spec.length > 0 &&
|
|
20
|
+
!spec.startsWith('.') && !spec.startsWith('/') && !spec.startsWith('#') &&
|
|
21
|
+
!/^[a-zA-Z][a-zA-Z0-9+.-]*:/.test(spec);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @param {string} sourceText
|
|
26
|
+
* @returns {{ builds: Set<string>, imports: string[], bareImports: string[] }}
|
|
27
|
+
*/
|
|
28
|
+
export function analyzeSource(sourceText) {
|
|
29
|
+
const builds = new Set();
|
|
30
|
+
const imports = new Set();
|
|
31
|
+
const bareImports = new Set();
|
|
32
|
+
|
|
33
|
+
const addImport = (node) => {
|
|
34
|
+
const src = node?.source?.value;
|
|
35
|
+
if (typeof src !== 'string') return;
|
|
36
|
+
if (src.startsWith('.')) imports.add(src);
|
|
37
|
+
else if (isBare(src)) bareImports.add(src);
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
try {
|
|
41
|
+
const ast = parse(sourceText, { sourceType: 'module', plugins: ['jsx'] });
|
|
42
|
+
|
|
43
|
+
traverse.default(ast, {
|
|
44
|
+
CallExpression(path) {
|
|
45
|
+
const { callee, arguments: args } = path.node;
|
|
46
|
+
|
|
47
|
+
// slice.build('Name', ...)
|
|
48
|
+
if (
|
|
49
|
+
callee.type === 'MemberExpression' &&
|
|
50
|
+
callee.object?.name === 'slice' &&
|
|
51
|
+
callee.property?.name === 'build' &&
|
|
52
|
+
args[0]?.type === 'StringLiteral'
|
|
53
|
+
) {
|
|
54
|
+
builds.add(args[0].value);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// dynamic import('./x.js') or import('some-pkg')
|
|
59
|
+
if (callee.type === 'Import' && args[0]?.type === 'StringLiteral') {
|
|
60
|
+
const spec = args[0].value;
|
|
61
|
+
if (spec.startsWith('.')) imports.add(spec);
|
|
62
|
+
else if (isBare(spec)) bareImports.add(spec);
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
|
|
66
|
+
ImportDeclaration(path) { addImport(path.node); },
|
|
67
|
+
ExportNamedDeclaration(path) { addImport(path.node); },
|
|
68
|
+
ExportAllDeclaration(path) { addImport(path.node); }
|
|
69
|
+
});
|
|
70
|
+
} catch {
|
|
71
|
+
// Unparseable source → discover nothing; the component still installs as-is.
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return { builds, imports: Array.from(imports), bareImports: Array.from(bareImports) };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export default analyzeSource;
|