@salty-css/core 0.1.0-alpha.5 → 0.1.0-alpha.7
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/astro-component-5hrNTCJ5.js +4 -0
- package/astro-component-Dj3enX6K.cjs +4 -0
- package/bin/commands/build.d.ts +2 -0
- package/bin/commands/generate.d.ts +2 -0
- package/bin/commands/init.d.ts +2 -0
- package/bin/commands/update.d.ts +2 -0
- package/bin/commands/version.d.ts +2 -0
- package/bin/context.d.ts +19 -0
- package/bin/detection/css-file.d.ts +5 -0
- package/bin/frameworks/astro.d.ts +4 -0
- package/bin/frameworks/index.d.ts +13 -0
- package/bin/frameworks/react.d.ts +2 -0
- package/bin/frameworks/types.d.ts +27 -0
- package/bin/integrations/astro.d.ts +11 -0
- package/bin/integrations/eslint.d.ts +6 -0
- package/bin/integrations/index.d.ts +13 -0
- package/bin/integrations/next.d.ts +9 -0
- package/bin/integrations/types.d.ts +17 -0
- package/bin/integrations/vite.d.ts +8 -0
- package/bin/main.cjs +548 -332
- package/bin/main.d.ts +8 -0
- package/bin/main.js +548 -332
- package/bin/package-json.d.ts +21 -0
- package/bin/saltyrc.d.ts +31 -0
- package/bin/templates.d.ts +14 -0
- package/package.json +1 -1
- package/styled-file-BzmB9_Ez.cjs +12 -0
- package/{react-styled-file-U02jek-B.cjs → styled-file-CPd_rTW2.cjs} +2 -2
- package/{react-styled-file-B99mwk0w.js → styled-file-Cda3EeR6.js} +2 -2
- package/styled-file-DLcgYmGN.js +12 -0
- package/{react-vanilla-file-D9px70iK.js → vanilla-file-1kOqbCIM.js} +2 -2
- package/{react-vanilla-file-Bj6XC8GS.cjs → vanilla-file-r0fp2q_m.cjs} +2 -2
package/bin/main.js
CHANGED
|
@@ -1,14 +1,43 @@
|
|
|
1
1
|
import { Command } from "commander";
|
|
2
2
|
import { existsSync, watch } from "fs";
|
|
3
|
-
import { mkdir, readFile, writeFile } from "fs/promises";
|
|
4
|
-
import { join, relative, parse, format } from "path";
|
|
5
|
-
import ejs from "ejs";
|
|
6
|
-
import { p as pascalCase } from "../pascal-case-F3Usi5Wf.js";
|
|
7
3
|
import { l as logger, a as logError, SaltyCompiler } from "../compiler/salty-compiler.js";
|
|
4
|
+
import { isSaltyFile } from "../compiler/helpers.js";
|
|
5
|
+
import { c as checkShouldRestart } from "../should-restart-CXIO0jxY.js";
|
|
6
|
+
import { join, relative, parse, format } from "path";
|
|
7
|
+
import { readFile, writeFile, mkdir } from "fs/promises";
|
|
8
8
|
import { exec } from "child_process";
|
|
9
9
|
import ora from "ora";
|
|
10
|
-
import {
|
|
11
|
-
import
|
|
10
|
+
import { p as pascalCase } from "../pascal-case-F3Usi5Wf.js";
|
|
11
|
+
import ejs from "ejs";
|
|
12
|
+
const defaultPackageJsonPath = join(process.cwd(), "package.json");
|
|
13
|
+
const readPackageJson = async (filePath = defaultPackageJsonPath) => {
|
|
14
|
+
const content = await readFile(filePath, "utf-8").then(JSON.parse).catch(() => void 0);
|
|
15
|
+
if (!content) throw "Could not read package.json file!";
|
|
16
|
+
return content;
|
|
17
|
+
};
|
|
18
|
+
const updatePackageJson = async (content, filePath = defaultPackageJsonPath) => {
|
|
19
|
+
if (typeof content === "object") content = JSON.stringify(content, null, 2);
|
|
20
|
+
await writeFile(filePath, content);
|
|
21
|
+
};
|
|
22
|
+
const readThisPackageJson = async () => {
|
|
23
|
+
const packageJsonPath = new URL("../package.json", import.meta.url);
|
|
24
|
+
return readPackageJson(packageJsonPath);
|
|
25
|
+
};
|
|
26
|
+
const corePackages = {
|
|
27
|
+
core: (version) => `@salty-css/core@${version}`,
|
|
28
|
+
eslintConfigCore: (version) => `@salty-css/eslint-config-core@${version}`
|
|
29
|
+
};
|
|
30
|
+
const addPrepareScript = (pkg) => {
|
|
31
|
+
if (!pkg.scripts) pkg.scripts = {};
|
|
32
|
+
const current = pkg.scripts["prepare"];
|
|
33
|
+
if (current) {
|
|
34
|
+
if (current.includes("salty-css")) return { changed: false, pkg };
|
|
35
|
+
pkg.scripts["prepare"] = current + " && npx salty-css build";
|
|
36
|
+
} else {
|
|
37
|
+
pkg.scripts["prepare"] = "npx salty-css build";
|
|
38
|
+
}
|
|
39
|
+
return { changed: true, pkg };
|
|
40
|
+
};
|
|
12
41
|
const execAsync = (command) => {
|
|
13
42
|
return new Promise((resolve, reject) => {
|
|
14
43
|
exec(command, (error) => {
|
|
@@ -37,365 +66,542 @@ async function formatWithPrettier(filePath) {
|
|
|
37
66
|
logger.error(`Error formatting ${filePath} with Prettier:`, error);
|
|
38
67
|
}
|
|
39
68
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
const packageJsonContent = await readFile(filePath, "utf-8").then(JSON.parse).catch(() => void 0);
|
|
64
|
-
if (!packageJsonContent) throw "Could not read package.json file!";
|
|
65
|
-
return packageJsonContent;
|
|
66
|
-
};
|
|
67
|
-
const updatePackageJson = async (content, filePath = defaultPackageJsonPath) => {
|
|
68
|
-
if (typeof content === "object") content = JSON.stringify(content, null, 2);
|
|
69
|
-
await writeFile(filePath, content);
|
|
70
|
-
};
|
|
71
|
-
const readThisPackageJson = async () => {
|
|
72
|
-
const packageJsonPath = new URL("../package.json", import.meta.url);
|
|
73
|
-
return readPackageJson(packageJsonPath);
|
|
74
|
-
};
|
|
75
|
-
const getDefaultProject = async () => {
|
|
76
|
-
const rcContent = await readRCFile();
|
|
77
|
-
return rcContent.defaultProject;
|
|
78
|
-
};
|
|
79
|
-
const defaultProject = await getDefaultProject();
|
|
80
|
-
const currentPackageJson = await readThisPackageJson();
|
|
81
|
-
const packages = {
|
|
82
|
-
core: `@salty-css/core@${currentPackageJson.version}`,
|
|
83
|
-
react: `@salty-css/react@${currentPackageJson.version}`,
|
|
84
|
-
eslintConfigCore: `@salty-css/eslint-config-core@${currentPackageJson.version}`,
|
|
85
|
-
vite: `@salty-css/vite@${currentPackageJson.version}`,
|
|
86
|
-
next: `@salty-css/next@${currentPackageJson.version}`
|
|
87
|
-
};
|
|
88
|
-
const resolveProjectDir = (dir) => {
|
|
89
|
-
const dirName = dir === "." ? "" : dir;
|
|
90
|
-
const rootDir = process.cwd();
|
|
91
|
-
const projectDir = join(rootDir, dirName);
|
|
92
|
-
return projectDir;
|
|
93
|
-
};
|
|
94
|
-
program.command("init [directory]").description("Initialize a new Salty-CSS project.").option("-d, --dir <dir>", "Project directory to initialize the project in.").option("--css-file <css-file>", "Existing CSS file where to import the generated CSS. Path must be relative to the given project directory.").option("--skip-install", "Skip installing dependencies.").action(async function(_dir = ".") {
|
|
95
|
-
const packageJson = await readPackageJson().catch(() => void 0);
|
|
96
|
-
if (!packageJson) return logError("Salty CSS project must be initialized in a directory with a package.json file.");
|
|
97
|
-
logger.info("Initializing a new Salty-CSS project!");
|
|
98
|
-
const { dir = _dir, cssFile, skipInstall } = this.opts();
|
|
99
|
-
if (!dir) return logError("Project directory must be provided. Add it as the first argument after init command or use the --dir option.");
|
|
100
|
-
if (!skipInstall) await npmInstall(packages.core, packages.react);
|
|
101
|
-
const rootDir = process.cwd();
|
|
102
|
-
const projectDir = resolveProjectDir(dir);
|
|
103
|
-
const projectFiles = await Promise.all([readTemplate("salty.config.ts"), readTemplate("saltygen/index.css")]);
|
|
104
|
-
const saltyCompiler = new SaltyCompiler(projectDir);
|
|
105
|
-
await mkdir(projectDir, { recursive: true });
|
|
106
|
-
const writeFiles = projectFiles.map(async ({ fileName, content }) => {
|
|
107
|
-
const filePath = join(projectDir, fileName);
|
|
108
|
-
const existingContent = await readFile(filePath, "utf-8").catch(() => void 0);
|
|
109
|
-
if (existingContent !== void 0) {
|
|
110
|
-
logger.debug("File already exists: " + filePath);
|
|
111
|
-
return;
|
|
112
|
-
}
|
|
113
|
-
const additionalFolders = fileName.split("/").slice(0, -1).join("/");
|
|
114
|
-
if (additionalFolders) await mkdir(join(projectDir, additionalFolders), { recursive: true });
|
|
115
|
-
logger.info("Creating file: " + filePath);
|
|
116
|
-
await writeFile(filePath, content);
|
|
117
|
-
await formatWithPrettier(filePath);
|
|
118
|
-
});
|
|
119
|
-
await Promise.all(writeFiles);
|
|
120
|
-
const relativeProjectPath = relative(rootDir, projectDir) || ".";
|
|
121
|
-
const saltyrcPath = join(rootDir, ".saltyrc.json");
|
|
122
|
-
const existingSaltyrc = await readFile(saltyrcPath, "utf-8").catch(() => void 0);
|
|
123
|
-
if (existingSaltyrc === void 0) {
|
|
124
|
-
logger.info("Creating file: " + saltyrcPath);
|
|
125
|
-
const rcContent = {
|
|
126
|
-
$schema: "./node_modules/@salty-css/core/.saltyrc.schema.json",
|
|
127
|
-
info: "This file is used to define projects and their configurations for Salty CSS cli. Do not delete, modify or add this file to .gitignore.",
|
|
128
|
-
defaultProject: relativeProjectPath,
|
|
129
|
-
projects: [
|
|
130
|
-
{
|
|
131
|
-
dir: relativeProjectPath,
|
|
132
|
-
framework: "react"
|
|
133
|
-
}
|
|
134
|
-
]
|
|
135
|
-
};
|
|
136
|
-
const content = JSON.stringify(rcContent, null, 2);
|
|
137
|
-
await writeFile(saltyrcPath, content);
|
|
138
|
-
await formatWithPrettier(saltyrcPath);
|
|
139
|
-
} else {
|
|
140
|
-
const rcContent = JSON.parse(existingSaltyrc);
|
|
141
|
-
const projects = (rcContent == null ? void 0 : rcContent.projects) || [];
|
|
142
|
-
const projectIndex = projects.findIndex((p) => p.dir === relativeProjectPath);
|
|
143
|
-
if (projectIndex === -1) {
|
|
144
|
-
projects.push({ dir: relativeProjectPath, framework: "react" });
|
|
145
|
-
rcContent.projects = [...projects];
|
|
146
|
-
const content = JSON.stringify(rcContent, null, 2);
|
|
147
|
-
if (content !== existingSaltyrc) {
|
|
148
|
-
logger.info("Edit file: " + saltyrcPath);
|
|
149
|
-
await writeFile(saltyrcPath, content);
|
|
150
|
-
await formatWithPrettier(saltyrcPath);
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
const gitIgnorePath = join(rootDir, ".gitignore");
|
|
155
|
-
const gitIgnoreContent = await readFile(gitIgnorePath, "utf-8").catch(() => void 0);
|
|
156
|
-
if (gitIgnoreContent !== void 0) {
|
|
157
|
-
const alreadyIgnoresSaltygen = gitIgnoreContent.includes("saltygen");
|
|
158
|
-
if (!alreadyIgnoresSaltygen) {
|
|
159
|
-
logger.info("Edit file: " + gitIgnorePath);
|
|
160
|
-
await writeFile(gitIgnorePath, gitIgnoreContent + "\n\n# Salty-CSS\nsaltygen\n");
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
const cssFileFoldersToLookFor = ["src", "public", "assets", "styles", "css", "app"];
|
|
164
|
-
const secondLevelFolders = ["styles", "css", "app", "pages"];
|
|
165
|
-
const cssFilesToLookFor = ["index", "styles", "main", "app", "global", "globals"];
|
|
166
|
-
const cssFileExtensions = [".css", ".scss", ".sass"];
|
|
167
|
-
const getTargetCssFile = async () => {
|
|
168
|
-
if (cssFile) return cssFile;
|
|
169
|
-
for (const folder of cssFileFoldersToLookFor) {
|
|
170
|
-
for (const file of cssFilesToLookFor) {
|
|
171
|
-
for (const ext of cssFileExtensions) {
|
|
172
|
-
const filePath = join(projectDir, folder, file + ext);
|
|
173
|
-
const fileContent = await readFile(filePath, "utf-8").catch(() => void 0);
|
|
174
|
-
if (fileContent !== void 0) return relative(projectDir, filePath);
|
|
175
|
-
for (const secondLevelFolder of secondLevelFolders) {
|
|
176
|
-
const filePath2 = join(projectDir, folder, secondLevelFolder, file + ext);
|
|
177
|
-
const fileContent2 = await readFile(filePath2, "utf-8").catch(() => void 0);
|
|
178
|
-
if (fileContent2 !== void 0) return relative(projectDir, filePath2);
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
return void 0;
|
|
184
|
-
};
|
|
185
|
-
const targetCSSFile = await getTargetCssFile();
|
|
186
|
-
if (targetCSSFile) {
|
|
187
|
-
const cssFilePath = join(projectDir, targetCSSFile);
|
|
188
|
-
const cssFileContent = await readFile(cssFilePath, "utf-8").catch(() => void 0);
|
|
189
|
-
if (cssFileContent !== void 0) {
|
|
190
|
-
const alreadyImportsSaltygen = cssFileContent.includes("saltygen");
|
|
191
|
-
if (!alreadyImportsSaltygen) {
|
|
192
|
-
const cssFileFolder = join(cssFilePath, "..");
|
|
193
|
-
const relativePath = relative(cssFileFolder, join(projectDir, "saltygen/index.css"));
|
|
194
|
-
const importStatement = `@import '${relativePath}';`;
|
|
195
|
-
logger.info("Adding global import statement to CSS file: " + cssFilePath);
|
|
196
|
-
await writeFile(cssFilePath, importStatement + "\n" + cssFileContent);
|
|
197
|
-
await formatWithPrettier(cssFilePath);
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
} else {
|
|
201
|
-
logger.warn("Could not find a CSS file to import the generated CSS. Please add it manually.");
|
|
202
|
-
}
|
|
203
|
-
const eslintConfigs = {
|
|
204
|
-
projectJs: join(projectDir, "eslint.config.js"),
|
|
205
|
-
rootJs: join(rootDir, "eslint.config.js"),
|
|
206
|
-
projectMjs: join(projectDir, "eslint.config.mjs"),
|
|
207
|
-
rootMjs: join(rootDir, "eslint.config.mjs"),
|
|
208
|
-
projectJson: join(projectDir, ".eslintrc.json"),
|
|
209
|
-
rootJson: join(rootDir, ".eslintrc.json")
|
|
69
|
+
const SALTYRC_FILENAME = ".saltyrc.json";
|
|
70
|
+
const SALTYRC_SCHEMA = "./node_modules/@salty-css/core/.saltyrc.schema.json";
|
|
71
|
+
const SALTYRC_INFO = "This file is used to define projects and their configurations for Salty CSS cli. Do not delete, modify or add this file to .gitignore.";
|
|
72
|
+
const saltyrcPath = (rootDir = process.cwd()) => join(rootDir, SALTYRC_FILENAME);
|
|
73
|
+
const readRc = async (rootDir = process.cwd()) => {
|
|
74
|
+
const content = await readFile(saltyrcPath(rootDir), "utf-8").then(JSON.parse).catch(() => ({}));
|
|
75
|
+
return content;
|
|
76
|
+
};
|
|
77
|
+
const readRawRc = async (rootDir = process.cwd()) => {
|
|
78
|
+
return readFile(saltyrcPath(rootDir), "utf-8").catch(() => void 0);
|
|
79
|
+
};
|
|
80
|
+
const getDefaultProject = async (rootDir = process.cwd()) => {
|
|
81
|
+
const rc = await readRc(rootDir);
|
|
82
|
+
return rc.defaultProject;
|
|
83
|
+
};
|
|
84
|
+
const upsertProjectInRc = (existingRaw, relativeProjectPath, framework) => {
|
|
85
|
+
const projectPath = join(relativeProjectPath, framework.srcDirectory);
|
|
86
|
+
if (existingRaw === void 0) {
|
|
87
|
+
const fresh = {
|
|
88
|
+
$schema: SALTYRC_SCHEMA,
|
|
89
|
+
info: SALTYRC_INFO,
|
|
90
|
+
defaultProject: projectPath,
|
|
91
|
+
projects: [{ dir: projectPath, framework: framework.name }]
|
|
210
92
|
};
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
let nextConfigContent = await readFile(nextConfigPath, "utf-8").catch(() => void 0);
|
|
264
|
-
if (nextConfigContent !== void 0) {
|
|
265
|
-
const alreadyHasPlugin = nextConfigContent.includes("withSaltyCss");
|
|
266
|
-
if (!alreadyHasPlugin) {
|
|
267
|
-
let saltyCssAppended = false;
|
|
268
|
-
const hasPluginsArray = /\splugins([^=]*)=[^[]\[/.test(nextConfigContent);
|
|
269
|
-
if (hasPluginsArray && !saltyCssAppended) {
|
|
270
|
-
nextConfigContent = nextConfigContent.replace(/\splugins([^=]*)=[^[]\[/, (_, config) => {
|
|
271
|
-
return ` plugins${config}= [withSaltyCss,`;
|
|
272
|
-
});
|
|
273
|
-
saltyCssAppended = true;
|
|
274
|
-
}
|
|
275
|
-
const useRequire = nextConfigContent.includes("module.exports");
|
|
276
|
-
const pluginImport = useRequire ? "const { withSaltyCss } = require('@salty-css/next');\n" : "import { withSaltyCss } from '@salty-css/next';\n";
|
|
277
|
-
if (useRequire && !saltyCssAppended) {
|
|
278
|
-
nextConfigContent = nextConfigContent.replace(/module.exports = ([^;]+)/, (_, config) => {
|
|
279
|
-
return `module.exports = withSaltyCss(${config})`;
|
|
280
|
-
});
|
|
281
|
-
saltyCssAppended = true;
|
|
282
|
-
} else if (!saltyCssAppended) {
|
|
283
|
-
nextConfigContent = nextConfigContent.replace(/export default ([^;]+)/, (_, config) => {
|
|
284
|
-
return `export default withSaltyCss(${config})`;
|
|
285
|
-
});
|
|
286
|
-
}
|
|
287
|
-
if (!skipInstall) await npmInstall(`-D ${packages.next}`);
|
|
288
|
-
logger.info("Adding Salty-CSS plugin to Next.js config...");
|
|
289
|
-
await writeFile(nextConfigPath, pluginImport + nextConfigContent);
|
|
290
|
-
await formatWithPrettier(nextConfigPath);
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
const packageJsonContent = await readPackageJson().catch(() => logError("Could not read package.json file.")).then((content) => {
|
|
295
|
-
if (!content.scripts) content.scripts = {};
|
|
296
|
-
if (content.scripts.prepare) {
|
|
297
|
-
const alreadyHasSaltyCss = content.scripts.prepare.includes("salty-css");
|
|
298
|
-
if (!alreadyHasSaltyCss) {
|
|
299
|
-
logger.info("Edit file: " + defaultPackageJsonPath);
|
|
300
|
-
content.scripts.prepare = content.scripts.prepare + " && npx salty-css build";
|
|
301
|
-
}
|
|
302
|
-
} else {
|
|
303
|
-
logger.info("Edit file: " + defaultPackageJsonPath);
|
|
304
|
-
content.scripts.prepare = "npx salty-css build";
|
|
305
|
-
}
|
|
306
|
-
return content;
|
|
307
|
-
});
|
|
308
|
-
await updatePackageJson(packageJsonContent);
|
|
309
|
-
logger.info("Running the build to generate initial CSS...");
|
|
310
|
-
await saltyCompiler.generateCss();
|
|
311
|
-
logger.info("🎉 Salty CSS project initialized successfully!");
|
|
312
|
-
logger.info("Next steps:");
|
|
313
|
-
logger.info("1. Configure variables and templates in `salty.config.ts`");
|
|
314
|
-
logger.info("2. Create a new component with `npx salty-css generate [component-name]`");
|
|
315
|
-
logger.info("3. Run `npx salty-css build` to generate the CSS");
|
|
316
|
-
logger.info("4. Read about the features in the documentation: https://salty-css.dev");
|
|
317
|
-
logger.info("5. Star the project on GitHub: https://github.com/margarita-form/salty-css ⭐");
|
|
318
|
-
});
|
|
93
|
+
return { content: JSON.stringify(fresh, null, 2), changed: true, created: true };
|
|
94
|
+
}
|
|
95
|
+
const rc = JSON.parse(existingRaw);
|
|
96
|
+
const projects = rc.projects || [];
|
|
97
|
+
const exists = projects.some((p) => p.dir === projectPath);
|
|
98
|
+
if (exists) return { content: existingRaw, changed: false, created: false };
|
|
99
|
+
projects.push({ dir: projectPath, framework: framework.name });
|
|
100
|
+
rc.projects = [...projects];
|
|
101
|
+
const next = JSON.stringify(rc, null, 2);
|
|
102
|
+
return { content: next, changed: next !== existingRaw, created: false };
|
|
103
|
+
};
|
|
104
|
+
const writeProjectToRc = async (cwd, relativeProjectPath, framework) => {
|
|
105
|
+
const path = saltyrcPath(cwd);
|
|
106
|
+
const existing = await readRawRc(cwd);
|
|
107
|
+
const { content, changed, created } = upsertProjectInRc(existing, relativeProjectPath, framework);
|
|
108
|
+
if (!changed) return false;
|
|
109
|
+
if (created) logger.info("Creating file: " + path);
|
|
110
|
+
else logger.info("Edit file: " + path);
|
|
111
|
+
await writeFile(path, content);
|
|
112
|
+
await formatWithPrettier(path);
|
|
113
|
+
return true;
|
|
114
|
+
};
|
|
115
|
+
const getProjectFramework = (rc, relativeProjectPath) => {
|
|
116
|
+
const projects = rc.projects || [];
|
|
117
|
+
const entry = projects.find((p) => p.dir === relativeProjectPath);
|
|
118
|
+
return entry == null ? void 0 : entry.framework;
|
|
119
|
+
};
|
|
120
|
+
const resolveProjectDir = (dir, rootDir = process.cwd()) => {
|
|
121
|
+
const dirName = dir === "." ? "" : dir;
|
|
122
|
+
return join(rootDir, dirName);
|
|
123
|
+
};
|
|
124
|
+
const buildContext = async (opts) => {
|
|
125
|
+
const cwd = process.cwd();
|
|
126
|
+
const projectDir = resolveProjectDir(opts.dir, cwd);
|
|
127
|
+
const relativeProjectPath = relative(cwd, projectDir) || ".";
|
|
128
|
+
const packageJson = await readPackageJson().catch(() => void 0);
|
|
129
|
+
if (opts.requirePackageJson !== false && !packageJson) {
|
|
130
|
+
throw new Error("Salty CSS project must be initialized in a directory with a package.json file.");
|
|
131
|
+
}
|
|
132
|
+
const rcFile = await readRc(cwd);
|
|
133
|
+
const cliPackageJson = await readThisPackageJson();
|
|
134
|
+
return {
|
|
135
|
+
cwd,
|
|
136
|
+
projectDir,
|
|
137
|
+
relativeProjectPath,
|
|
138
|
+
packageJson,
|
|
139
|
+
rcFile,
|
|
140
|
+
cliVersion: cliPackageJson.version || "0.0.0",
|
|
141
|
+
skipInstall: !!opts.skipInstall
|
|
142
|
+
};
|
|
143
|
+
};
|
|
144
|
+
const registerBuildCommand = (program, defaultProject) => {
|
|
319
145
|
program.command("build [directory]").alias("b").description("Build the Salty-CSS project.").option("-d, --dir <dir>", "Project directory to build the project in.").option("--watch", "Watch for changes and rebuild the project.").action(async function(_dir = defaultProject) {
|
|
320
146
|
logger.info("Building the Salty-CSS project...");
|
|
321
147
|
const { dir = _dir, watch: watch$1 } = this.opts();
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
const
|
|
325
|
-
|
|
148
|
+
const resolved = dir ?? await getDefaultProject();
|
|
149
|
+
if (!resolved) return logError("Project directory must be provided. Add it as the first argument after build command or use the --dir option.");
|
|
150
|
+
const projectDir = resolveProjectDir(resolved);
|
|
151
|
+
const compiler = new SaltyCompiler(projectDir);
|
|
152
|
+
await compiler.generateCss();
|
|
326
153
|
if (watch$1) {
|
|
327
154
|
logger.info("Watching for changes in the project directory...");
|
|
328
|
-
watch(projectDir, { recursive: true }, async (
|
|
155
|
+
watch(projectDir, { recursive: true }, async (_event, filePath) => {
|
|
329
156
|
const shouldRestart = await checkShouldRestart(filePath);
|
|
330
157
|
if (shouldRestart) {
|
|
331
|
-
await
|
|
332
|
-
} else {
|
|
333
|
-
|
|
334
|
-
if (saltyFile) await saltyCompiler.generateFile(filePath);
|
|
158
|
+
await compiler.generateCss(false);
|
|
159
|
+
} else if (isSaltyFile(filePath)) {
|
|
160
|
+
await compiler.generateFile(filePath);
|
|
335
161
|
}
|
|
336
162
|
});
|
|
337
163
|
}
|
|
338
164
|
});
|
|
339
|
-
|
|
165
|
+
};
|
|
166
|
+
const astroConfigFiles = ["astro.config.mjs", "astro.config.ts", "astro.config.js", "astro.config.cjs"];
|
|
167
|
+
const findAstroConfig = (projectDir) => {
|
|
168
|
+
for (const name of astroConfigFiles) {
|
|
169
|
+
const path = join(projectDir, name);
|
|
170
|
+
if (existsSync(path)) return path;
|
|
171
|
+
}
|
|
172
|
+
return null;
|
|
173
|
+
};
|
|
174
|
+
const hasAstroDependency = (ctx) => {
|
|
175
|
+
const pkg = ctx.packageJson;
|
|
176
|
+
if (!pkg) return false;
|
|
177
|
+
const all = { ...pkg.dependencies, ...pkg.devDependencies };
|
|
178
|
+
return Object.prototype.hasOwnProperty.call(all, "astro");
|
|
179
|
+
};
|
|
180
|
+
const astroFramework = {
|
|
181
|
+
name: "astro",
|
|
182
|
+
srcDirectory: "src",
|
|
183
|
+
detect: (ctx) => Boolean(findAstroConfig(ctx.projectDir)) || hasAstroDependency(ctx),
|
|
184
|
+
runtimePackage: (version) => `@salty-css/astro@${version}`,
|
|
185
|
+
templates: {
|
|
186
|
+
styled: "astro/styled-file.ts",
|
|
187
|
+
component: {
|
|
188
|
+
styled: "astro/styled-file.ts",
|
|
189
|
+
wrapper: "astro/component.astro",
|
|
190
|
+
wrapperExt: ".astro"
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
const reactFramework = {
|
|
195
|
+
name: "react",
|
|
196
|
+
srcDirectory: "",
|
|
197
|
+
detect: () => true,
|
|
198
|
+
// default fallback — evaluated last in the registry
|
|
199
|
+
runtimePackage: (version) => `@salty-css/react@${version}`,
|
|
200
|
+
templates: {
|
|
201
|
+
styled: "react/styled-file.ts",
|
|
202
|
+
component: {
|
|
203
|
+
styled: "react/styled-file.ts",
|
|
204
|
+
wrapper: "react/vanilla-file.ts",
|
|
205
|
+
wrapperExt: ".tsx"
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
const frameworkRegistry = [astroFramework, reactFramework];
|
|
210
|
+
const frameworksByName = {
|
|
211
|
+
astro: astroFramework,
|
|
212
|
+
react: reactFramework
|
|
213
|
+
};
|
|
214
|
+
const detectFramework = async (ctx) => {
|
|
215
|
+
for (const adapter of frameworkRegistry) {
|
|
216
|
+
if (await adapter.detect(ctx)) return adapter;
|
|
217
|
+
}
|
|
218
|
+
return reactFramework;
|
|
219
|
+
};
|
|
220
|
+
const getFramework = (name) => {
|
|
221
|
+
if (!name) return void 0;
|
|
222
|
+
return frameworksByName[name];
|
|
223
|
+
};
|
|
224
|
+
const templateLoaders = {
|
|
225
|
+
"salty.config.ts": () => import("../salty.config-DjosWdPw.js"),
|
|
226
|
+
"saltygen/index.css": () => import("../index-DKz1QXqs.js"),
|
|
227
|
+
"react/styled-file.ts": () => import("../styled-file-Cda3EeR6.js"),
|
|
228
|
+
"react/vanilla-file.ts": () => import("../vanilla-file-1kOqbCIM.js"),
|
|
229
|
+
"astro/styled-file.ts": () => import("../styled-file-DLcgYmGN.js"),
|
|
230
|
+
"astro/component.astro": () => import("../astro-component-5hrNTCJ5.js")
|
|
231
|
+
};
|
|
232
|
+
const readTemplate = async (key, options) => {
|
|
233
|
+
const { default: file } = await templateLoaders[key]();
|
|
234
|
+
const content = ejs.render(file, options);
|
|
235
|
+
return { fileName: key, content };
|
|
236
|
+
};
|
|
237
|
+
const registerGenerateCommand = (program, defaultProject) => {
|
|
238
|
+
program.command("generate [file] [directory]").alias("g").description("Generate a new component file.").option("-f, --file <file>", "File to generate.").option("-d, --dir <dir>", "Project directory to generate the file in.").option("-t, --tag <tag>", "HTML tag of the component.", "div").option("-n, --name <name>", "Name of the component.").option("-c, --className <className>", "CSS class of the component.").option("-r, --reactComponent", "Generate a wrapper component file alongside the styled definition.").action(async function(_file, _dir = defaultProject) {
|
|
340
239
|
const { file = _file, dir = _dir, tag, name, className, reactComponent = false } = this.opts();
|
|
341
240
|
if (!file) return logError("File to generate must be provided. Add it as the first argument after generate command or use the --file option.");
|
|
342
241
|
if (!dir) return logError("Project directory must be provided. Add it as the second argument after generate command or use the --dir option.");
|
|
343
|
-
|
|
242
|
+
let ctx;
|
|
243
|
+
try {
|
|
244
|
+
ctx = await buildContext({ dir, requirePackageJson: false });
|
|
245
|
+
} catch (err) {
|
|
246
|
+
return logError(err instanceof Error ? err.message : String(err));
|
|
247
|
+
}
|
|
248
|
+
const rcFramework = getFramework(getProjectFramework(ctx.rcFile, ctx.relativeProjectPath));
|
|
249
|
+
const framework = rcFramework ?? await detectFramework(ctx);
|
|
344
250
|
const additionalFolders = file.split("/").slice(0, -1).join("/");
|
|
345
|
-
if (additionalFolders) await mkdir(join(projectDir, additionalFolders), { recursive: true });
|
|
346
|
-
const filePath = join(projectDir, file);
|
|
251
|
+
if (additionalFolders) await mkdir(join(ctx.projectDir, additionalFolders), { recursive: true });
|
|
252
|
+
const filePath = join(ctx.projectDir, file);
|
|
347
253
|
const parsedFilePath = parse(filePath);
|
|
348
|
-
if (!parsedFilePath.ext)
|
|
349
|
-
|
|
350
|
-
}
|
|
351
|
-
if (!parsedFilePath.name.endsWith(".css")) {
|
|
352
|
-
parsedFilePath.name = parsedFilePath.name + ".css";
|
|
353
|
-
}
|
|
254
|
+
if (!parsedFilePath.ext) parsedFilePath.ext = ".ts";
|
|
255
|
+
if (!parsedFilePath.name.endsWith(".css")) parsedFilePath.name = parsedFilePath.name + ".css";
|
|
354
256
|
parsedFilePath.base = parsedFilePath.name + parsedFilePath.ext;
|
|
355
257
|
const formattedStyledFilePath = format(parsedFilePath);
|
|
356
258
|
const alreadyExists = await readFile(formattedStyledFilePath, "utf-8").catch(() => void 0);
|
|
357
259
|
if (alreadyExists !== void 0) {
|
|
358
|
-
logger.error("File already exists:"
|
|
260
|
+
logger.error("File already exists: " + formattedStyledFilePath);
|
|
359
261
|
return;
|
|
360
262
|
}
|
|
361
263
|
let styledComponentName = pascalCase(name || parsedFilePath.base.replace(/\.css\.\w+$/, ""));
|
|
362
264
|
if (reactComponent) {
|
|
265
|
+
if (!framework.templates.component) {
|
|
266
|
+
return logError(`--reactComponent is not supported for the ${framework.name} framework.`);
|
|
267
|
+
}
|
|
363
268
|
const componentName = styledComponentName + "Component";
|
|
364
269
|
styledComponentName = styledComponentName + "Wrapper";
|
|
365
270
|
const fileName = parsedFilePath.base.replace(/\.css\.\w+$/, "");
|
|
366
|
-
const { content:
|
|
271
|
+
const { content: wrapperContent } = await readTemplate(framework.templates.component.wrapper, {
|
|
272
|
+
tag,
|
|
273
|
+
componentName,
|
|
274
|
+
styledComponentName,
|
|
275
|
+
className,
|
|
276
|
+
fileName
|
|
277
|
+
});
|
|
367
278
|
parsedFilePath.name = fileName.replace(/\.css$/, "");
|
|
368
|
-
parsedFilePath.ext =
|
|
279
|
+
parsedFilePath.ext = framework.templates.component.wrapperExt;
|
|
369
280
|
parsedFilePath.base = parsedFilePath.name + parsedFilePath.ext;
|
|
370
|
-
const
|
|
371
|
-
logger.info("Generating a new file: " +
|
|
372
|
-
await writeFile(
|
|
373
|
-
await formatWithPrettier(
|
|
281
|
+
const formattedWrapperPath = format(parsedFilePath);
|
|
282
|
+
logger.info("Generating a new file: " + formattedWrapperPath);
|
|
283
|
+
await writeFile(formattedWrapperPath, wrapperContent);
|
|
284
|
+
await formatWithPrettier(formattedWrapperPath);
|
|
374
285
|
}
|
|
375
|
-
const
|
|
286
|
+
const styledKey = reactComponent && framework.templates.component ? framework.templates.component.styled : framework.templates.styled;
|
|
287
|
+
const { content } = await readTemplate(styledKey, { tag, name: styledComponentName, className });
|
|
376
288
|
logger.info("Generating a new file: " + formattedStyledFilePath);
|
|
377
289
|
await writeFile(formattedStyledFilePath, content);
|
|
378
290
|
await formatWithPrettier(formattedStyledFilePath);
|
|
379
291
|
});
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
292
|
+
};
|
|
293
|
+
const CSS_FILE_FOLDERS = ["src", "public", "assets", "styles", "css", "app"];
|
|
294
|
+
const CSS_SECOND_LEVEL_FOLDERS = ["styles", "css", "app", "pages"];
|
|
295
|
+
const CSS_FILE_NAMES = ["index", "styles", "main", "app", "global", "globals"];
|
|
296
|
+
const CSS_FILE_EXTENSIONS = [".css", ".scss", ".sass"];
|
|
297
|
+
const findGlobalCssFile = async (projectDir) => {
|
|
298
|
+
for (const folder of CSS_FILE_FOLDERS) {
|
|
299
|
+
for (const file of CSS_FILE_NAMES) {
|
|
300
|
+
for (const ext of CSS_FILE_EXTENSIONS) {
|
|
301
|
+
const filePath = join(projectDir, folder, file + ext);
|
|
302
|
+
const fileContent = await readFile(filePath, "utf-8").catch(() => void 0);
|
|
303
|
+
if (fileContent !== void 0) return relative(projectDir, filePath);
|
|
304
|
+
for (const second of CSS_SECOND_LEVEL_FOLDERS) {
|
|
305
|
+
const nestedPath = join(projectDir, folder, second, file + ext);
|
|
306
|
+
const nestedContent = await readFile(nestedPath, "utf-8").catch(() => void 0);
|
|
307
|
+
if (nestedContent !== void 0) return relative(projectDir, nestedPath);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
390
310
|
}
|
|
391
|
-
|
|
392
|
-
|
|
311
|
+
}
|
|
312
|
+
return void 0;
|
|
313
|
+
};
|
|
314
|
+
const astroPackage = (version) => `@salty-css/astro@${version}`;
|
|
315
|
+
const SALTY_ASTRO_IMPORT = "import saltyIntegration from '@salty-css/astro/integration';\n";
|
|
316
|
+
const editAstroConfig = (existing) => {
|
|
317
|
+
if (existing.includes("@salty-css/astro")) return { content: null };
|
|
318
|
+
let next = existing;
|
|
319
|
+
let inserted = false;
|
|
320
|
+
if (/integrations\s*:\s*\[/.test(next)) {
|
|
321
|
+
next = next.replace(/integrations\s*:\s*\[/, (m) => `${m}saltyIntegration(),`);
|
|
322
|
+
inserted = true;
|
|
323
|
+
} else if (/defineConfig\s*\(\s*\{/.test(next)) {
|
|
324
|
+
next = next.replace(/defineConfig\s*\(\s*\{/, (m) => `${m}
|
|
325
|
+
integrations: [saltyIntegration()],`);
|
|
326
|
+
inserted = true;
|
|
327
|
+
}
|
|
328
|
+
if (!inserted) {
|
|
329
|
+
return {
|
|
330
|
+
content: null,
|
|
331
|
+
warning: "Could not find a place to add saltyIntegration() in the Astro config. Please add it manually."
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
return { content: SALTY_ASTRO_IMPORT + next };
|
|
335
|
+
};
|
|
336
|
+
const astroIntegration = {
|
|
337
|
+
name: "astro",
|
|
338
|
+
detect: (ctx) => findAstroConfig(ctx.projectDir),
|
|
339
|
+
apply: async (ctx, configPath) => {
|
|
340
|
+
const existing = await readFile(configPath, "utf-8").catch(() => void 0);
|
|
341
|
+
if (existing === void 0) return { changed: false };
|
|
342
|
+
const result = editAstroConfig(existing);
|
|
343
|
+
if (result.warning) logger.warn(result.warning);
|
|
344
|
+
if (result.content === null) return { changed: false };
|
|
345
|
+
if (!ctx.skipInstall) await npmInstall(`-D ${astroPackage(ctx.cliVersion)}`);
|
|
346
|
+
logger.info("Adding Salty-CSS integration to Astro config: " + configPath);
|
|
347
|
+
await writeFile(configPath, result.content);
|
|
348
|
+
await formatWithPrettier(configPath);
|
|
349
|
+
return { changed: true };
|
|
350
|
+
}
|
|
351
|
+
};
|
|
352
|
+
const ESLINT_CONFIG_CANDIDATES = [
|
|
353
|
+
// Project-local candidates first, then root-level.
|
|
354
|
+
["projectJs", "eslint.config.js"],
|
|
355
|
+
["rootJs", "eslint.config.js"],
|
|
356
|
+
["projectMjs", "eslint.config.mjs"],
|
|
357
|
+
["rootMjs", "eslint.config.mjs"],
|
|
358
|
+
["projectJson", ".eslintrc.json"],
|
|
359
|
+
["rootJson", ".eslintrc.json"]
|
|
360
|
+
];
|
|
361
|
+
const eslintConfigCandidates = (projectDir, rootDir) => {
|
|
362
|
+
return ESLINT_CONFIG_CANDIDATES.map(([scope, name]) => {
|
|
363
|
+
const base = scope.startsWith("root") ? rootDir : projectDir;
|
|
364
|
+
return join(base, name);
|
|
365
|
+
});
|
|
366
|
+
};
|
|
367
|
+
const editEslintConfig = (existing, isJsFlat) => {
|
|
368
|
+
if (existing.includes("salty-css")) return { content: null };
|
|
369
|
+
if (isJsFlat) {
|
|
370
|
+
const importStatement = 'import saltyCss from "@salty-css/eslint-config-core/flat";';
|
|
371
|
+
let newContent = `${importStatement}
|
|
372
|
+
${existing}`;
|
|
373
|
+
const isTsEslint = existing.includes("typescript-eslint");
|
|
374
|
+
if (isTsEslint) {
|
|
375
|
+
if (newContent.includes(".config(")) {
|
|
376
|
+
newContent = newContent.replace(".config(", ".config(saltyCss,");
|
|
377
|
+
} else {
|
|
378
|
+
return {
|
|
379
|
+
content: null,
|
|
380
|
+
warning: "Could not find the correct place to add the Salty-CSS config for ESLint. Please add it manually."
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
} else {
|
|
384
|
+
if (newContent.includes("export default [")) {
|
|
385
|
+
newContent = newContent.replace("export default [", "export default [ saltyCss,");
|
|
386
|
+
} else if (newContent.includes("eslintConfig = [")) {
|
|
387
|
+
newContent = newContent.replace("eslintConfig = [", "eslintConfig = [ saltyCss,");
|
|
388
|
+
} else {
|
|
389
|
+
return {
|
|
390
|
+
content: null,
|
|
391
|
+
warning: "Could not find the correct place to add the Salty-CSS config for ESLint. Please add it manually."
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
return { content: newContent };
|
|
396
|
+
}
|
|
397
|
+
const json = JSON.parse(existing);
|
|
398
|
+
if (!json.extends) json.extends = [];
|
|
399
|
+
if (!json.extends.includes("@salty-css/core")) json.extends.push("@salty-css/core");
|
|
400
|
+
return { content: JSON.stringify(json, null, 2) };
|
|
401
|
+
};
|
|
402
|
+
const eslintIntegration = {
|
|
403
|
+
name: "eslint",
|
|
404
|
+
detect: (ctx) => {
|
|
405
|
+
const candidates = eslintConfigCandidates(ctx.projectDir, ctx.cwd);
|
|
406
|
+
return candidates.find((p) => existsSync(p)) ?? null;
|
|
407
|
+
},
|
|
408
|
+
apply: async (ctx, configPath) => {
|
|
409
|
+
const existing = await readFile(configPath, "utf-8").catch(() => void 0);
|
|
410
|
+
if (existing === void 0) {
|
|
411
|
+
logger.error("Could not read ESLint config file.");
|
|
412
|
+
return { changed: false };
|
|
413
|
+
}
|
|
414
|
+
if (!ctx.skipInstall) await npmInstall(corePackages.eslintConfigCore(ctx.cliVersion));
|
|
415
|
+
const result = editEslintConfig(existing, configPath.endsWith("js"));
|
|
416
|
+
if (result.warning) logger.warn(result.warning);
|
|
417
|
+
if (result.content === null) return { changed: false };
|
|
418
|
+
logger.info("Edit file: " + configPath);
|
|
419
|
+
await writeFile(configPath, result.content);
|
|
420
|
+
await formatWithPrettier(configPath);
|
|
421
|
+
return { changed: true };
|
|
422
|
+
}
|
|
423
|
+
};
|
|
424
|
+
const nextConfigFiles = ["next.config.js", "next.config.cjs", "next.config.ts", "next.config.mjs"];
|
|
425
|
+
const nextPackage = (version) => `@salty-css/next@${version}`;
|
|
426
|
+
const editNextConfig = (existing) => {
|
|
427
|
+
if (existing.includes("withSaltyCss")) return { content: null };
|
|
428
|
+
let next = existing;
|
|
429
|
+
let saltyCssAppended = false;
|
|
430
|
+
const hasPluginsArray = /\splugins([^=]*)=[^[]\[/.test(next);
|
|
431
|
+
if (hasPluginsArray) {
|
|
432
|
+
next = next.replace(/\splugins([^=]*)=[^[]\[/, (_, config) => ` plugins${config}= [withSaltyCss,`);
|
|
433
|
+
saltyCssAppended = true;
|
|
434
|
+
}
|
|
435
|
+
const useRequire = next.includes("module.exports");
|
|
436
|
+
const pluginImport = useRequire ? "const { withSaltyCss } = require('@salty-css/next');\n" : "import { withSaltyCss } from '@salty-css/next';\n";
|
|
437
|
+
if (useRequire && !saltyCssAppended) {
|
|
438
|
+
next = next.replace(/module.exports = ([^;]+)/, (_, config) => `module.exports = withSaltyCss(${config})`);
|
|
439
|
+
saltyCssAppended = true;
|
|
440
|
+
} else if (!saltyCssAppended) {
|
|
441
|
+
next = next.replace(/export default ([^;]+)/, (_, config) => `export default withSaltyCss(${config})`);
|
|
442
|
+
}
|
|
443
|
+
return { content: pluginImport + next };
|
|
444
|
+
};
|
|
445
|
+
const nextIntegration = {
|
|
446
|
+
name: "next",
|
|
447
|
+
detect: (ctx) => {
|
|
448
|
+
const found = nextConfigFiles.map((file) => join(ctx.projectDir, file)).find((p) => existsSync(p));
|
|
449
|
+
return found ?? null;
|
|
450
|
+
},
|
|
451
|
+
apply: async (ctx, configPath) => {
|
|
452
|
+
const existing = await readFile(configPath, "utf-8").catch(() => void 0);
|
|
453
|
+
if (existing === void 0) return { changed: false };
|
|
454
|
+
const { content } = editNextConfig(existing);
|
|
455
|
+
if (content === null) return { changed: false };
|
|
456
|
+
if (!ctx.skipInstall) await npmInstall(`-D ${nextPackage(ctx.cliVersion)}`);
|
|
457
|
+
logger.info("Adding Salty-CSS plugin to Next.js config...");
|
|
458
|
+
await writeFile(configPath, content);
|
|
459
|
+
await formatWithPrettier(configPath);
|
|
460
|
+
return { changed: true };
|
|
461
|
+
}
|
|
462
|
+
};
|
|
463
|
+
const vitePackage = (version) => `@salty-css/vite@${version}`;
|
|
464
|
+
const editViteConfig = (existing) => {
|
|
465
|
+
if (existing.includes("saltyPlugin")) return { content: null };
|
|
466
|
+
const pluginImport = "import { saltyPlugin } from '@salty-css/vite';\n";
|
|
467
|
+
const pluginConfig = "saltyPlugin(__dirname),";
|
|
468
|
+
const withPlugin = existing.replace(/(plugins: \[)/, `$1
|
|
469
|
+
${pluginConfig}`);
|
|
470
|
+
return { content: pluginImport + withPlugin };
|
|
471
|
+
};
|
|
472
|
+
const viteIntegration = {
|
|
473
|
+
name: "vite",
|
|
474
|
+
detect: (ctx) => {
|
|
475
|
+
const path = join(ctx.projectDir, "vite.config.ts");
|
|
476
|
+
return existsSync(path) ? path : null;
|
|
477
|
+
},
|
|
478
|
+
apply: async (ctx, configPath) => {
|
|
479
|
+
const existing = await readFile(configPath, "utf-8").catch(() => void 0);
|
|
480
|
+
if (existing === void 0) return { changed: false };
|
|
481
|
+
const { content } = editViteConfig(existing);
|
|
482
|
+
if (content === null) return { changed: false };
|
|
483
|
+
logger.info("Edit file: " + configPath);
|
|
484
|
+
if (!ctx.skipInstall) await npmInstall(`-D ${vitePackage(ctx.cliVersion)}`);
|
|
485
|
+
logger.info("Adding Salty-CSS plugin to Vite config...");
|
|
486
|
+
await writeFile(configPath, content);
|
|
487
|
+
await formatWithPrettier(configPath);
|
|
488
|
+
return { changed: true };
|
|
489
|
+
}
|
|
490
|
+
};
|
|
491
|
+
const buildIntegrationRegistry = [eslintIntegration, viteIntegration, nextIntegration, astroIntegration];
|
|
492
|
+
const detectAndApplyIntegrations = async (ctx) => {
|
|
493
|
+
const results = [];
|
|
494
|
+
for (const integration of buildIntegrationRegistry) {
|
|
495
|
+
const configPath = await integration.detect(ctx);
|
|
496
|
+
if (!configPath) continue;
|
|
497
|
+
const result = await integration.apply(ctx, configPath);
|
|
498
|
+
results.push({ name: integration.name, configPath, changed: result.changed });
|
|
499
|
+
}
|
|
500
|
+
return results;
|
|
501
|
+
};
|
|
502
|
+
const writeProjectFile = async (projectDir, fileName, content) => {
|
|
503
|
+
const filePath = join(projectDir, fileName);
|
|
504
|
+
if (existsSync(filePath)) {
|
|
505
|
+
logger.debug("File already exists: " + filePath);
|
|
506
|
+
return;
|
|
507
|
+
}
|
|
508
|
+
const additionalFolders = fileName.split("/").slice(0, -1).join("/");
|
|
509
|
+
if (additionalFolders) await mkdir(join(projectDir, additionalFolders), { recursive: true });
|
|
510
|
+
logger.info("Creating file: " + filePath);
|
|
511
|
+
await writeFile(filePath, content);
|
|
512
|
+
await formatWithPrettier(filePath);
|
|
513
|
+
};
|
|
514
|
+
const ensureGitignoreSaltygen = async (rootDir) => {
|
|
515
|
+
const path = join(rootDir, ".gitignore");
|
|
516
|
+
const existing = await readFile(path, "utf-8").catch(() => void 0);
|
|
517
|
+
if (existing === void 0) return;
|
|
518
|
+
if (existing.includes("saltygen")) return;
|
|
519
|
+
logger.info("Edit file: " + path);
|
|
520
|
+
await writeFile(path, existing + "\n\n# Salty-CSS\nsaltygen\n");
|
|
521
|
+
};
|
|
522
|
+
const importSaltygenIntoCss = async (projectDir, explicitCssFile) => {
|
|
523
|
+
const target = explicitCssFile ?? await findGlobalCssFile(projectDir);
|
|
524
|
+
if (!target) {
|
|
525
|
+
logger.warn("Could not find a CSS file to import the generated CSS. Please add it manually.");
|
|
526
|
+
return;
|
|
527
|
+
}
|
|
528
|
+
const cssFilePath = join(projectDir, target);
|
|
529
|
+
const cssFileContent = await readFile(cssFilePath, "utf-8").catch(() => void 0);
|
|
530
|
+
if (cssFileContent === void 0) return;
|
|
531
|
+
if (cssFileContent.includes("saltygen")) return;
|
|
532
|
+
const cssFileFolder = join(cssFilePath, "..");
|
|
533
|
+
const relPath = relative(cssFileFolder, join(projectDir, "saltygen/index.css"));
|
|
534
|
+
logger.info("Adding global import statement to CSS file: " + cssFilePath);
|
|
535
|
+
await writeFile(cssFilePath, `@import '${relPath}';
|
|
536
|
+
` + cssFileContent);
|
|
537
|
+
await formatWithPrettier(cssFilePath);
|
|
538
|
+
};
|
|
539
|
+
const wirePrepareScript = async () => {
|
|
540
|
+
const pkg = await readPackageJson().catch(() => {
|
|
541
|
+
logError("Could not read package.json file.");
|
|
542
|
+
return void 0;
|
|
543
|
+
});
|
|
544
|
+
if (!pkg) return;
|
|
545
|
+
const { pkg: next } = addPrepareScript(pkg);
|
|
546
|
+
await updatePackageJson(next);
|
|
547
|
+
};
|
|
548
|
+
const registerInitCommand = (program) => {
|
|
549
|
+
program.command("init [directory]").description("Initialize a new Salty-CSS project.").option("-d, --dir <dir>", "Project directory to initialize the project in.").option("--css-file <css-file>", "Existing CSS file where to import the generated CSS. Path must be relative to the given project directory.").option("--skip-install", "Skip installing dependencies.").action(async function(_dir = ".") {
|
|
550
|
+
try {
|
|
551
|
+
const opts = this.opts();
|
|
552
|
+
const dir = opts.dir ?? _dir;
|
|
553
|
+
if (!dir) return logError("Project directory must be provided. Add it as the first argument after init command or use the --dir option.");
|
|
554
|
+
const ctx = await buildContext({ dir, skipInstall: opts.skipInstall });
|
|
555
|
+
logger.info("Initializing a new Salty-CSS project!");
|
|
556
|
+
const framework = await detectFramework(ctx);
|
|
557
|
+
logger.info(`Detected framework: ${framework.name}`);
|
|
558
|
+
if (!ctx.skipInstall) {
|
|
559
|
+
await npmInstall(corePackages.core(ctx.cliVersion), framework.runtimePackage(ctx.cliVersion));
|
|
560
|
+
}
|
|
561
|
+
const projectFiles = await Promise.all([readTemplate("salty.config.ts"), readTemplate("saltygen/index.css")]);
|
|
562
|
+
await mkdir(ctx.projectDir, { recursive: true });
|
|
563
|
+
await Promise.all(projectFiles.map(({ fileName, content }) => writeProjectFile(ctx.projectDir, fileName, content)));
|
|
564
|
+
await writeProjectToRc(ctx.cwd, ctx.relativeProjectPath, framework);
|
|
565
|
+
await ensureGitignoreSaltygen(ctx.cwd);
|
|
566
|
+
await importSaltygenIntoCss(ctx.projectDir, opts.cssFile);
|
|
567
|
+
await detectAndApplyIntegrations(ctx);
|
|
568
|
+
await wirePrepareScript();
|
|
569
|
+
logger.info("Running the build to generate initial CSS...");
|
|
570
|
+
const compiler = new SaltyCompiler(ctx.projectDir);
|
|
571
|
+
await compiler.generateCss();
|
|
572
|
+
logger.info("🎉 Salty CSS project initialized successfully!");
|
|
573
|
+
logger.info("Next steps:");
|
|
574
|
+
logger.info("1. Configure variables and templates in `salty.config.ts`");
|
|
575
|
+
logger.info("2. Create a new component with `npx salty-css generate [component-name]`");
|
|
576
|
+
logger.info("3. Run `npx salty-css build` to generate the CSS");
|
|
577
|
+
logger.info("4. Read about the features in the documentation: https://salty-css.dev");
|
|
578
|
+
logger.info("5. Star the project on GitHub: https://github.com/margarita-form/salty-css ⭐");
|
|
579
|
+
} catch (err) {
|
|
580
|
+
return logError(err instanceof Error ? err.message : String(err));
|
|
581
|
+
}
|
|
582
|
+
});
|
|
583
|
+
};
|
|
584
|
+
const getSaltyCssPackages = async () => {
|
|
585
|
+
const packageJSONPath = join(process.cwd(), "package.json");
|
|
586
|
+
const packageJson = await readPackageJson(packageJSONPath).catch((err) => logError(err));
|
|
587
|
+
if (!packageJson) return logError("Could not read package.json file.");
|
|
588
|
+
const allDependencies = { ...packageJson.dependencies, ...packageJson.devDependencies };
|
|
589
|
+
const saltyCssPackages = Object.entries(allDependencies).filter(([name]) => name === "salty-css" || name.startsWith("@salty-css/"));
|
|
590
|
+
if (!saltyCssPackages.length) {
|
|
591
|
+
return logError(
|
|
592
|
+
"No Salty-CSS packages found in package.json. Make sure you are running update command in the same directory! Used package.json path: " + packageJSONPath
|
|
593
|
+
);
|
|
594
|
+
}
|
|
595
|
+
return saltyCssPackages;
|
|
596
|
+
};
|
|
597
|
+
const registerUpdateCommand = (program) => {
|
|
393
598
|
program.command("update [version]").alias("up").description("Update Salty-CSS packages to the latest or specified version.").option("-v, --version <version>", "Version to update to.").option("--legacy-peer-deps <legacyPeerDeps>", "Use legacy peer dependencies (not recommended).", false).action(async function(_version = "latest") {
|
|
394
599
|
const { legacyPeerDeps, version = _version } = this.opts();
|
|
395
600
|
const saltyCssPackages = await getSaltyCssPackages();
|
|
396
601
|
if (!saltyCssPackages) return logError("Could not update Salty-CSS packages as any were found in package.json.");
|
|
602
|
+
const cli = await readThisPackageJson();
|
|
397
603
|
const packagesToUpdate = saltyCssPackages.map(([name]) => {
|
|
398
|
-
if (version === "@") return `${name}@${
|
|
604
|
+
if (version === "@") return `${name}@${cli.version}`;
|
|
399
605
|
return `${name}@${version.replace(/^@/, "")}`;
|
|
400
606
|
});
|
|
401
607
|
if (legacyPeerDeps) {
|
|
@@ -413,19 +619,19 @@ ${eslintConfigContent}`;
|
|
|
413
619
|
}, {});
|
|
414
620
|
const versionsCount = Object.keys(mappedByVersions).length;
|
|
415
621
|
if (versionsCount === 1) {
|
|
416
|
-
const
|
|
417
|
-
|
|
418
|
-
logger.info(`Updated to all Salty CSS packages successfully to ${versionString}`);
|
|
622
|
+
const v = Object.keys(mappedByVersions)[0];
|
|
623
|
+
logger.info(`Updated to all Salty CSS packages successfully to ${v.replace(/^\^/, "")}`);
|
|
419
624
|
} else {
|
|
420
|
-
for (const [
|
|
421
|
-
|
|
422
|
-
logger.info(`Updated to ${versionString}: ${names.join(", ")}`);
|
|
625
|
+
for (const [v, names] of Object.entries(mappedByVersions)) {
|
|
626
|
+
logger.info(`Updated to ${v.replace(/^\^/, "")}: ${names.join(", ")}`);
|
|
423
627
|
}
|
|
424
628
|
}
|
|
425
629
|
});
|
|
630
|
+
};
|
|
631
|
+
const registerVersionOption = (program) => {
|
|
426
632
|
program.option("-v, --version", "Show the current version of Salty-CSS.").action(async function() {
|
|
427
|
-
const
|
|
428
|
-
logger.info("CLI is running: " +
|
|
633
|
+
const cli = await readThisPackageJson();
|
|
634
|
+
logger.info("CLI is running: " + cli.version);
|
|
429
635
|
const packageJSONPath = join(process.cwd(), "package.json");
|
|
430
636
|
const packageJson = await readPackageJson(packageJSONPath).catch((err) => logError(err));
|
|
431
637
|
if (!packageJson) return;
|
|
@@ -440,6 +646,16 @@ ${eslintConfigContent}`;
|
|
|
440
646
|
logger.info(`${dep}: ${allDependencies[dep]}`);
|
|
441
647
|
}
|
|
442
648
|
});
|
|
649
|
+
};
|
|
650
|
+
async function main() {
|
|
651
|
+
const program = new Command();
|
|
652
|
+
program.name("salty-css").description("Salty-CSS CLI tool to help with annoying configuration tasks.");
|
|
653
|
+
const defaultProject = await getDefaultProject();
|
|
654
|
+
registerInitCommand(program);
|
|
655
|
+
registerBuildCommand(program, defaultProject);
|
|
656
|
+
registerGenerateCommand(program, defaultProject);
|
|
657
|
+
registerUpdateCommand(program);
|
|
658
|
+
registerVersionOption(program);
|
|
443
659
|
program.parseAsync(process.argv);
|
|
444
660
|
}
|
|
445
661
|
export {
|