@snap/valdi 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.bazelignore +2 -0
- package/.bootstrap/apps/cli_application/AGENTS.md.template +215 -0
- package/.bootstrap/apps/cli_application/BUILD.bazel.template +7 -0
- package/.bootstrap/apps/cli_application/modules/{{MODULE_NAME}}/BUILD.bazel.template +11 -0
- package/.bootstrap/apps/cli_application/modules/{{MODULE_NAME}}/src/index.ts +1 -0
- package/.bootstrap/apps/ui_application/.eslint.rc +27 -0
- package/.bootstrap/apps/ui_application/AGENTS.md.template +202 -0
- package/.bootstrap/apps/ui_application/BUILD.bazel.template +19 -0
- package/.bootstrap/apps/ui_application/modules/{{MODULE_NAME}}/BUILD.bazel.template +21 -0
- package/.bootstrap/apps/ui_application/modules/{{MODULE_NAME}}/res/valdi@3x.png +0 -0
- package/.bootstrap/apps/ui_application/modules/{{MODULE_NAME}}/src/App.tsx +133 -0
- package/.bootstrap/apps/ui_application/package.json +24 -0
- package/.bootstrap/modules/cpp_bridge_module/BUILD.bazel.template +21 -0
- package/.bootstrap/modules/cpp_bridge_module/cpp/CalculatorModuleFactory.cpp.template +29 -0
- package/.bootstrap/modules/cpp_bridge_module/src/Calculator.d.ts +1 -0
- package/.bootstrap/modules/ios_android_bridge_module/BUILD.bazel.template +39 -0
- package/.bootstrap/modules/ios_android_bridge_module/android/CalculatorModuleFactory.kt.template +18 -0
- package/.bootstrap/modules/ios_android_bridge_module/ios/SCCCalculatorModuleFactory.m.template +31 -0
- package/.bootstrap/modules/ios_android_bridge_module/src/Calculator.d.ts +4 -0
- package/.bootstrap/modules/ios_android_view_module/BUILD.bazel.template +41 -0
- package/.bootstrap/modules/ios_android_view_module/android/CircleView.kt.template +42 -0
- package/.bootstrap/modules/ios_android_view_module/android/CircleViewAttributesBinder.kt.template +60 -0
- package/.bootstrap/modules/ios_android_view_module/ios/SCCCircleView.h +6 -0
- package/.bootstrap/modules/ios_android_view_module/ios/SCCCircleView.m +82 -0
- package/.bootstrap/modules/ios_android_view_module/src/Circle.tsx.template +15 -0
- package/.bootstrap/modules/ui_component/BUILD.bazel.template +34 -0
- package/.bootstrap/modules/ui_component/src/NewModuleComponent.tsx +45 -0
- package/.config/copyconfig.json +10 -0
- package/.editorconfig +21 -0
- package/.eslintignore +1 -0
- package/.eslintrc.js +72 -0
- package/.metadata/.bazelrc.template +91 -0
- package/.metadata/.bazelversion.template +1 -0
- package/.metadata/.eslintrc.js.template +27 -0
- package/.metadata/.gitignore.template +4 -0
- package/.metadata/.prettierrc.json.template +7 -0
- package/.metadata/.watchmanconfig.template +8 -0
- package/.metadata/README.md.template +31 -0
- package/.metadata/WORKSPACE.template +57 -0
- package/.metadata/config.yaml.template +1 -0
- package/.metadata/package.json.template +23 -0
- package/.prettierignore +2 -0
- package/.prettierrc.json +7 -0
- package/README.md +123 -0
- package/dist/commands/bootstrap.js +233 -0
- package/dist/commands/build.js +67 -0
- package/dist/commands/devSetup.js +18 -0
- package/dist/commands/doctor.js +892 -0
- package/dist/commands/export.js +84 -0
- package/dist/commands/hotreload.js +89 -0
- package/dist/commands/install.js +71 -0
- package/dist/commands/lint.js +18 -0
- package/dist/commands/lint_commands/lintCheck.js +31 -0
- package/dist/commands/lint_commands/lintFormat.js +31 -0
- package/dist/commands/log.js +84 -0
- package/dist/commands/newModule.js +208 -0
- package/dist/commands/package.js +66 -0
- package/dist/commands/projectsync.js +316 -0
- package/dist/commands/test.js +54 -0
- package/dist/core/constants.js +76 -0
- package/dist/core/errors.js +12 -0
- package/dist/index.js +22 -0
- package/dist/setup/DevSetupHelper.js +146 -0
- package/dist/setup/linuxSetup.js +31 -0
- package/dist/setup/macOSSetup.js +35 -0
- package/dist/setup/setupEntryPoint.js +56 -0
- package/dist/setup/versions.js +8 -0
- package/dist/utils/ArgumentsResolver.js +23 -0
- package/dist/utils/BazelClient.js +219 -0
- package/dist/utils/Digraph.js +71 -0
- package/dist/utils/LoadingIndicator.js +38 -0
- package/dist/utils/applicationUtils.js +201 -0
- package/dist/utils/buildInfo.js +60 -0
- package/dist/utils/byString.js +6 -0
- package/dist/utils/cliUtils.js +93 -0
- package/dist/utils/copyBootstrapFiles.js +26 -0
- package/dist/utils/deviceUtils.js +72 -0
- package/dist/utils/errorUtils.js +55 -0
- package/dist/utils/fileUtils.js +204 -0
- package/dist/utils/githubUtils.js +8 -0
- package/dist/utils/jsonUtils.js +28 -0
- package/dist/utils/lintUtils.js +149 -0
- package/dist/utils/logUtils.js +8 -0
- package/dist/utils/pathUtils.js +12 -0
- package/dist/utils/stringUtils.js +17 -0
- package/dist/utils/stringUtils.spec.js +18 -0
- package/dist/utils/tempDir.js +26 -0
- package/dist/utils/zipUtils.js +16 -0
- package/package.json +51 -0
- package/prettier.config.js +4 -0
- package/spec/support/jasmine.json +6 -0
- package/src/commands/bootstrap.ts +317 -0
- package/src/commands/build.ts +89 -0
- package/src/commands/devSetup.ts +17 -0
- package/src/commands/doctor.ts +1422 -0
- package/src/commands/export.ts +131 -0
- package/src/commands/hotreload.ts +108 -0
- package/src/commands/install.ts +93 -0
- package/src/commands/lint.ts +15 -0
- package/src/commands/lint_commands/lintCheck.ts +53 -0
- package/src/commands/lint_commands/lintFormat.ts +49 -0
- package/src/commands/log.ts +108 -0
- package/src/commands/newModule.ts +287 -0
- package/src/commands/package.ts +102 -0
- package/src/commands/projectsync.ts +488 -0
- package/src/commands/test.ts +68 -0
- package/src/core/constants.ts +95 -0
- package/src/core/errors.ts +10 -0
- package/src/index.ts +22 -0
- package/src/setup/DevSetupHelper.ts +188 -0
- package/src/setup/linuxSetup.ts +38 -0
- package/src/setup/macOSSetup.ts +42 -0
- package/src/setup/setupEntryPoint.ts +64 -0
- package/src/setup/versions.ts +8 -0
- package/src/utils/ArgumentsResolver.ts +23 -0
- package/src/utils/BazelClient.ts +285 -0
- package/src/utils/Digraph.ts +81 -0
- package/src/utils/LoadingIndicator.ts +43 -0
- package/src/utils/applicationUtils.ts +276 -0
- package/src/utils/buildInfo.ts +99 -0
- package/src/utils/byString.ts +3 -0
- package/src/utils/cliUtils.ts +112 -0
- package/src/utils/copyBootstrapFiles.ts +30 -0
- package/src/utils/deviceUtils.ts +76 -0
- package/src/utils/errorUtils.ts +63 -0
- package/src/utils/fileUtils.ts +257 -0
- package/src/utils/githubUtils.ts +6 -0
- package/src/utils/jsonUtils.ts +30 -0
- package/src/utils/lintUtils.ts +190 -0
- package/src/utils/logUtils.ts +9 -0
- package/src/utils/pathUtils.ts +12 -0
- package/src/utils/stringUtils.spec.ts +19 -0
- package/src/utils/stringUtils.ts +15 -0
- package/src/utils/tempDir.ts +24 -0
- package/src/utils/zipUtils.ts +14 -0
- package/test/ValdiSmokeTest.ts +278 -0
- package/test/helpers/AsyncHelpers.ts +113 -0
- package/test/helpers/CommandHelpers.ts +37 -0
- package/test/helpers/ProjectHelpers.ts +153 -0
- package/test/helpers/StreamHelpers.ts +37 -0
- package/test/helpers/TypeScriptClient.ts +217 -0
- package/tsconfig.dist.json +8 -0
- package/tsconfig.json +42 -0
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TemplateFile = void 0;
|
|
4
|
+
exports.resolveFilePath = resolveFilePath;
|
|
5
|
+
exports.fileExists = fileExists;
|
|
6
|
+
exports.getUserConfig = getUserConfig;
|
|
7
|
+
exports.getAllFilePaths = getAllFilePaths;
|
|
8
|
+
exports.isDirectoryEmpty = isDirectoryEmpty;
|
|
9
|
+
exports.getFilesSortedByUpdatedTime = getFilesSortedByUpdatedTime;
|
|
10
|
+
exports.isDirectory = isDirectory;
|
|
11
|
+
exports.hasExtension = hasExtension;
|
|
12
|
+
exports.processReplacements = processReplacements;
|
|
13
|
+
exports.copyFileWithReplacement = copyFileWithReplacement;
|
|
14
|
+
exports.deleteAll = deleteAll;
|
|
15
|
+
exports.loadConfig = loadConfig;
|
|
16
|
+
exports.copyFiles = copyFiles;
|
|
17
|
+
exports.removeFileOrDirAtPath = removeFileOrDirAtPath;
|
|
18
|
+
const tslib_1 = require("tslib");
|
|
19
|
+
const fs_1 = tslib_1.__importDefault(require("fs"));
|
|
20
|
+
const os_1 = tslib_1.__importDefault(require("os"));
|
|
21
|
+
const path_1 = tslib_1.__importDefault(require("path"));
|
|
22
|
+
const yaml_1 = tslib_1.__importDefault(require("yaml"));
|
|
23
|
+
const Constants = tslib_1.__importStar(require("../core/constants"));
|
|
24
|
+
const errors_1 = require("../core/errors");
|
|
25
|
+
class TemplateFile {
|
|
26
|
+
templatePath;
|
|
27
|
+
metaTemplatePath;
|
|
28
|
+
outputPath;
|
|
29
|
+
replacements;
|
|
30
|
+
baseName;
|
|
31
|
+
basePath;
|
|
32
|
+
constructor(templatePath, outputPath, replacements = {}) {
|
|
33
|
+
this.templatePath = path_1.default.normalize(templatePath);
|
|
34
|
+
this.metaTemplatePath = path_1.default.join(Constants.META_DIR_PATH, this.templatePath);
|
|
35
|
+
if (!fileExists(this.metaTemplatePath)) {
|
|
36
|
+
throw new errors_1.CliError(`Template file not found: ${this.metaTemplatePath}`);
|
|
37
|
+
}
|
|
38
|
+
const parsedPath = path_1.default.parse(this.templatePath);
|
|
39
|
+
this.baseName = parsedPath.name;
|
|
40
|
+
this.basePath = path_1.default.join(parsedPath.dir, parsedPath.name);
|
|
41
|
+
this.outputPath = outputPath ?? path_1.default.join(process.cwd(), this.basePath);
|
|
42
|
+
this.outputPath = path_1.default.normalize(this.outputPath);
|
|
43
|
+
this.replacements = replacements;
|
|
44
|
+
}
|
|
45
|
+
static init(templatePath) {
|
|
46
|
+
return new TemplateFile(templatePath);
|
|
47
|
+
}
|
|
48
|
+
withReplacements(replacements) {
|
|
49
|
+
return new TemplateFile(this.templatePath, this.outputPath, replacements);
|
|
50
|
+
}
|
|
51
|
+
withOutputPath(outputPath) {
|
|
52
|
+
const newOutputPath = isDirectory(outputPath) ? path_1.default.join(outputPath, this.baseName) : outputPath;
|
|
53
|
+
return new TemplateFile(this.templatePath, newOutputPath, this.replacements);
|
|
54
|
+
}
|
|
55
|
+
expandTemplate() {
|
|
56
|
+
const metaTemplatePath = path_1.default.join(Constants.META_DIR_PATH, this.templatePath);
|
|
57
|
+
return copyFileWithReplacement(metaTemplatePath, this.outputPath, this.replacements);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
exports.TemplateFile = TemplateFile;
|
|
61
|
+
function resolveFilePath(pathString) {
|
|
62
|
+
if (pathString.startsWith('~')) {
|
|
63
|
+
const tildeResolvedPath = path_1.default.join(os_1.default.homedir(), pathString.slice(1));
|
|
64
|
+
return path_1.default.resolve(tildeResolvedPath);
|
|
65
|
+
}
|
|
66
|
+
return path_1.default.resolve(pathString);
|
|
67
|
+
}
|
|
68
|
+
function fileExists(filePath) {
|
|
69
|
+
return fs_1.default.existsSync(filePath);
|
|
70
|
+
}
|
|
71
|
+
function getUserConfig() {
|
|
72
|
+
let resolvedPath;
|
|
73
|
+
for (const filePath of Constants.VALDI_CONFIG_PATHS) {
|
|
74
|
+
const unrolledPath = resolveFilePath(filePath);
|
|
75
|
+
if (fs_1.default.existsSync(unrolledPath)) {
|
|
76
|
+
resolvedPath = unrolledPath;
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (resolvedPath === undefined) {
|
|
81
|
+
throw new errors_1.CliError(`Please create a Valdi config file in ${Constants.VALDI_CONFIG_PATHS[0] ?? ''} (Running 'valdi bootstrap' takes care of that for you)`);
|
|
82
|
+
}
|
|
83
|
+
const configContents = fs_1.default.readFileSync(resolvedPath, 'utf8');
|
|
84
|
+
const parsedYaml = yaml_1.default.parse(configContents);
|
|
85
|
+
return parsedYaml;
|
|
86
|
+
}
|
|
87
|
+
function getAllFilePaths(directoryPath) {
|
|
88
|
+
directoryPath = resolveFilePath(directoryPath);
|
|
89
|
+
const files = [];
|
|
90
|
+
function traverseDirectories(dir) {
|
|
91
|
+
fs_1.default.readdirSync(dir, { withFileTypes: true }).forEach(file => {
|
|
92
|
+
const fullPath = path_1.default.join(dir, file.name);
|
|
93
|
+
if (file.isDirectory()) {
|
|
94
|
+
traverseDirectories(fullPath);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
files.push(fullPath);
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
traverseDirectories(directoryPath);
|
|
101
|
+
return files;
|
|
102
|
+
}
|
|
103
|
+
function isDirectoryEmpty(directoryPath) {
|
|
104
|
+
const absPath = resolveFilePath(directoryPath);
|
|
105
|
+
const dir = fs_1.default.opendirSync(absPath);
|
|
106
|
+
const firstEntry = dir.readSync();
|
|
107
|
+
dir.closeSync();
|
|
108
|
+
return firstEntry === null;
|
|
109
|
+
}
|
|
110
|
+
function getFilesSortedByUpdatedTime(directoryPath) {
|
|
111
|
+
directoryPath = resolveFilePath(directoryPath);
|
|
112
|
+
const files = getAllFilePaths(directoryPath);
|
|
113
|
+
const fileStats = files.map(file => {
|
|
114
|
+
const stats = fs_1.default.statSync(file);
|
|
115
|
+
return { file, mtime: stats.mtime };
|
|
116
|
+
});
|
|
117
|
+
const sortedFiles = fileStats.sort((a, b) => b.mtime.getTime() - a.mtime.getTime()).map(fileStat => fileStat.file);
|
|
118
|
+
return sortedFiles;
|
|
119
|
+
}
|
|
120
|
+
function isDirectory(filePath) {
|
|
121
|
+
return fs_1.default.existsSync(filePath) && fs_1.default.statSync(filePath).isDirectory();
|
|
122
|
+
}
|
|
123
|
+
function hasExtension(filePath, extension) {
|
|
124
|
+
return fs_1.default.existsSync(filePath) && path_1.default.extname(filePath) === extension;
|
|
125
|
+
}
|
|
126
|
+
function processReplacements(input, replacements) {
|
|
127
|
+
let outputContent = input;
|
|
128
|
+
for (const [key, value] of Object.entries(replacements)) {
|
|
129
|
+
const placeholder = `{{${key}}}`;
|
|
130
|
+
const regex = new RegExp(placeholder, 'g');
|
|
131
|
+
outputContent = outputContent.replace(regex, value);
|
|
132
|
+
}
|
|
133
|
+
return outputContent;
|
|
134
|
+
}
|
|
135
|
+
function copyFileWithReplacement(inputFilePath, outputFilePath, replacements) {
|
|
136
|
+
const inputContent = fs_1.default.readFileSync(inputFilePath, 'utf8');
|
|
137
|
+
const outputContent = processReplacements(inputContent, replacements);
|
|
138
|
+
const outputDir = path_1.default.dirname(outputFilePath);
|
|
139
|
+
if (!fs_1.default.existsSync(outputDir)) {
|
|
140
|
+
fs_1.default.mkdirSync(outputDir, { recursive: true });
|
|
141
|
+
}
|
|
142
|
+
fs_1.default.writeFileSync(outputFilePath, outputContent);
|
|
143
|
+
return outputFilePath;
|
|
144
|
+
}
|
|
145
|
+
function deleteAll(destPath) {
|
|
146
|
+
if (!isDirectory(destPath)) {
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
fs_1.default.readdirSync(destPath, { withFileTypes: true }).forEach(item => {
|
|
150
|
+
const fullPath = path_1.default.join(destPath, item.name);
|
|
151
|
+
fs_1.default.rmSync(fullPath, { recursive: true });
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
function loadConfig(configPath) {
|
|
155
|
+
const ext = path_1.default.extname(configPath).toLowerCase();
|
|
156
|
+
const rawConfig = fs_1.default.readFileSync(configPath, 'utf8');
|
|
157
|
+
if (ext === '.json') {
|
|
158
|
+
return JSON.parse(rawConfig);
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
throw new Error(`Unsupported config file format: ${ext}`);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
function precompilePatterns(patterns) {
|
|
165
|
+
return patterns?.map(pattern => new RegExp(pattern)) ?? [];
|
|
166
|
+
}
|
|
167
|
+
function matchesCompiledPatterns(compiledPatterns, filePath) {
|
|
168
|
+
return compiledPatterns.some(regex => regex.test(filePath));
|
|
169
|
+
}
|
|
170
|
+
function isPathAllowed(filePath, blacklist, whitelist) {
|
|
171
|
+
const normalizedPath = resolveFilePath(filePath);
|
|
172
|
+
if (blacklist.length > 0 && matchesCompiledPatterns(blacklist, normalizedPath)) {
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
if (whitelist.length > 0) {
|
|
176
|
+
return matchesCompiledPatterns(whitelist, normalizedPath);
|
|
177
|
+
}
|
|
178
|
+
return true;
|
|
179
|
+
}
|
|
180
|
+
function copyFiles(sourceDir, destDir, config = {}) {
|
|
181
|
+
const blacklist = precompilePatterns(config.blacklist);
|
|
182
|
+
const whitelist = precompilePatterns(config.whitelist);
|
|
183
|
+
fs_1.default.cpSync(sourceDir, destDir, {
|
|
184
|
+
recursive: true,
|
|
185
|
+
filter: (src) => {
|
|
186
|
+
return isPathAllowed(src, blacklist, whitelist);
|
|
187
|
+
},
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
function removeFileOrDirAtPath(path) {
|
|
191
|
+
if (!fs_1.default.existsSync(path)) {
|
|
192
|
+
return false;
|
|
193
|
+
}
|
|
194
|
+
const outputStat = fs_1.default.lstatSync(path);
|
|
195
|
+
if (outputStat.isFile()) {
|
|
196
|
+
fs_1.default.rmSync(path);
|
|
197
|
+
return true;
|
|
198
|
+
}
|
|
199
|
+
else if (outputStat.isDirectory()) {
|
|
200
|
+
fs_1.default.rmSync(path, { recursive: true });
|
|
201
|
+
return true;
|
|
202
|
+
}
|
|
203
|
+
return false;
|
|
204
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.resolveLatestReleaseRef = resolveLatestReleaseRef;
|
|
4
|
+
const cliUtils_1 = require("./cliUtils");
|
|
5
|
+
async function resolveLatestReleaseRef(repoUrl) {
|
|
6
|
+
const result = await (0, cliUtils_1.runCliCommand)(`git ls-remote --tags --refs ${repoUrl}`, undefined, true);
|
|
7
|
+
return result.stdout.trim().split(/\s+/)[1];
|
|
8
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.JSONCFile = void 0;
|
|
4
|
+
const jsonc_parser_1 = require("jsonc-parser");
|
|
5
|
+
class JSONCFile {
|
|
6
|
+
content;
|
|
7
|
+
value;
|
|
8
|
+
constructor(content, value) {
|
|
9
|
+
this.content = content;
|
|
10
|
+
this.value = value;
|
|
11
|
+
}
|
|
12
|
+
static parse(content) {
|
|
13
|
+
const parsed = (0, jsonc_parser_1.parse)(content);
|
|
14
|
+
return new JSONCFile(content, parsed);
|
|
15
|
+
}
|
|
16
|
+
static fromObject(object) {
|
|
17
|
+
const content = JSON.stringify(object, null, 2);
|
|
18
|
+
return JSONCFile.parse(content);
|
|
19
|
+
}
|
|
20
|
+
toJSONString() {
|
|
21
|
+
return this.content;
|
|
22
|
+
}
|
|
23
|
+
updating(path, value, options) {
|
|
24
|
+
const edits = (0, jsonc_parser_1.modify)(this.content, path, value, options);
|
|
25
|
+
return new JSONCFile((0, jsonc_parser_1.applyEdits)(this.content, edits), this.value);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
exports.JSONCFile = JSONCFile;
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RESULT_STATUS = void 0;
|
|
4
|
+
exports.maybeSetupPrettier = maybeSetupPrettier;
|
|
5
|
+
exports.getPrettierConfigPath = getPrettierConfigPath;
|
|
6
|
+
exports.createPrettierConfig = createPrettierConfig;
|
|
7
|
+
exports.maybeSetupEslint = maybeSetupEslint;
|
|
8
|
+
exports.createEslintConfig = createEslintConfig;
|
|
9
|
+
exports.createPackageConfig = createPackageConfig;
|
|
10
|
+
exports.getPrettierConfig = getPrettierConfig;
|
|
11
|
+
exports.checkAndFormatFiles = checkAndFormatFiles;
|
|
12
|
+
exports.printStatus = printStatus;
|
|
13
|
+
const tslib_1 = require("tslib");
|
|
14
|
+
const fs_1 = tslib_1.__importDefault(require("fs"));
|
|
15
|
+
const path_1 = tslib_1.__importDefault(require("path"));
|
|
16
|
+
const eslint_1 = tslib_1.__importStar(require("eslint"));
|
|
17
|
+
const prettier_1 = tslib_1.__importDefault(require("prettier"));
|
|
18
|
+
const constants_1 = require("../core/constants");
|
|
19
|
+
const errors_1 = require("../core/errors");
|
|
20
|
+
const cliUtils_1 = require("./cliUtils");
|
|
21
|
+
const fileUtils_1 = require("./fileUtils");
|
|
22
|
+
const logUtils_1 = require("./logUtils");
|
|
23
|
+
var RESULT_STATUS;
|
|
24
|
+
(function (RESULT_STATUS) {
|
|
25
|
+
RESULT_STATUS["SKIPPED"] = "skipped";
|
|
26
|
+
RESULT_STATUS["DIRECTORY"] = "directory";
|
|
27
|
+
RESULT_STATUS["NEEDS_FORMAT"] = "needs format";
|
|
28
|
+
RESULT_STATUS["VALID"] = "valid";
|
|
29
|
+
RESULT_STATUS["CHANGED"] = "changed";
|
|
30
|
+
})(RESULT_STATUS || (exports.RESULT_STATUS = RESULT_STATUS = {}));
|
|
31
|
+
async function maybeSetupPrettier() {
|
|
32
|
+
let prettierConfigPath = await getPrettierConfigPath();
|
|
33
|
+
if (prettierConfigPath === null) {
|
|
34
|
+
const initMessage = `No prettier config found. Would you like to create one here? ${(0, logUtils_1.wrapInColor)(process.cwd(), constants_1.ANSI_COLORS.GREEN_COLOR)}`;
|
|
35
|
+
const shouldInitPrettier = await (0, cliUtils_1.getUserConfirmation)(initMessage, true);
|
|
36
|
+
if (!shouldInitPrettier) {
|
|
37
|
+
throw new errors_1.CliError('Prettier config is required, aborting...');
|
|
38
|
+
}
|
|
39
|
+
prettierConfigPath = createPrettierConfig();
|
|
40
|
+
await prettier_1.default.clearConfigCache();
|
|
41
|
+
}
|
|
42
|
+
return prettierConfigPath;
|
|
43
|
+
}
|
|
44
|
+
async function getPrettierConfigPath() {
|
|
45
|
+
const prettierConfigPath = await prettier_1.default.resolveConfigFile();
|
|
46
|
+
return prettierConfigPath;
|
|
47
|
+
}
|
|
48
|
+
function createPrettierConfig() {
|
|
49
|
+
const prettierTemplateFile = fileUtils_1.TemplateFile.init(constants_1.TEMPLATE_BASE_PATHS.PRETTIER_CONFIG);
|
|
50
|
+
return prettierTemplateFile.expandTemplate();
|
|
51
|
+
}
|
|
52
|
+
async function maybeSetupEslint() {
|
|
53
|
+
if (!(0, fileUtils_1.fileExists)('.eslintrc.js')) {
|
|
54
|
+
const initMessage = `No eslintrc found. Would you like to create one here? ${(0, logUtils_1.wrapInColor)(process.cwd(), constants_1.ANSI_COLORS.GREEN_COLOR)}`;
|
|
55
|
+
const shouldInitEslintrc = await (0, cliUtils_1.getUserConfirmation)(initMessage, true);
|
|
56
|
+
if (!shouldInitEslintrc) {
|
|
57
|
+
throw new errors_1.CliError('eslintrc config is required, aborting...');
|
|
58
|
+
}
|
|
59
|
+
createEslintConfig();
|
|
60
|
+
}
|
|
61
|
+
if (!(0, fileUtils_1.fileExists)('package.json')) {
|
|
62
|
+
const initMessage = `No package.json found. Would you like to create one here? ${(0, logUtils_1.wrapInColor)(process.cwd(), constants_1.ANSI_COLORS.GREEN_COLOR)}`;
|
|
63
|
+
const shouldInitEslintrc = await (0, cliUtils_1.getUserConfirmation)(initMessage, true);
|
|
64
|
+
if (!shouldInitEslintrc) {
|
|
65
|
+
throw new errors_1.CliError('package.json config is required, aborting...');
|
|
66
|
+
}
|
|
67
|
+
createPackageConfig();
|
|
68
|
+
throw new errors_1.CliError('New package.json created, run `npm install` before trying again.');
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
function createEslintConfig() {
|
|
72
|
+
const eslintConfigFile = fileUtils_1.TemplateFile.init(constants_1.TEMPLATE_BASE_PATHS.ESLINT_CONFIG);
|
|
73
|
+
return eslintConfigFile.expandTemplate();
|
|
74
|
+
}
|
|
75
|
+
function createPackageConfig() {
|
|
76
|
+
const packageConfigFile = fileUtils_1.TemplateFile.init(constants_1.TEMPLATE_BASE_PATHS.ESLINT_PACKAGE_JSON_CONFIG);
|
|
77
|
+
return packageConfigFile.expandTemplate();
|
|
78
|
+
}
|
|
79
|
+
async function getPrettierConfig(configPath) {
|
|
80
|
+
return await prettier_1.default.resolveConfig(configPath);
|
|
81
|
+
}
|
|
82
|
+
async function checkAndFormatFiles(filePaths, prettierOptions, shouldFormat = false) {
|
|
83
|
+
const linter = new eslint_1.default.ESLint({ fix: true });
|
|
84
|
+
const results = await Promise.all(filePaths.map(async (fp) => {
|
|
85
|
+
const filePath = path_1.default.resolve(fp);
|
|
86
|
+
if ((0, fileUtils_1.isDirectory)(filePath)) {
|
|
87
|
+
return [fp, RESULT_STATUS.DIRECTORY];
|
|
88
|
+
}
|
|
89
|
+
if ((0, fileUtils_1.hasExtension)(filePath, '.json')) {
|
|
90
|
+
return [fp, RESULT_STATUS.SKIPPED];
|
|
91
|
+
}
|
|
92
|
+
const fileContent = fs_1.default.readFileSync(filePath, 'utf8');
|
|
93
|
+
const prettierFileInfo = await prettier_1.default.getFileInfo(filePath);
|
|
94
|
+
if (prettierFileInfo.inferredParser === null) {
|
|
95
|
+
return [fp, RESULT_STATUS.SKIPPED];
|
|
96
|
+
}
|
|
97
|
+
const lintResults = await linter.lintFiles([filePath]);
|
|
98
|
+
const needsLint = lintResults.length > 0;
|
|
99
|
+
let isLintFormatted = false;
|
|
100
|
+
if (shouldFormat && needsLint) {
|
|
101
|
+
console.log(lintResults[0]?.messages[0]?.message);
|
|
102
|
+
await eslint_1.ESLint.outputFixes(lintResults);
|
|
103
|
+
isLintFormatted = true;
|
|
104
|
+
}
|
|
105
|
+
const needsPrettier = await prettier_1.default.check(fileContent, { ...prettierOptions, filepath: filePath });
|
|
106
|
+
let isPrettierFormatted = false;
|
|
107
|
+
if (shouldFormat && !needsPrettier) {
|
|
108
|
+
const formattedContent = await prettier_1.default.format(fileContent, { ...prettierOptions, filepath: filePath });
|
|
109
|
+
fs_1.default.writeFileSync(filePath, formattedContent, 'utf8');
|
|
110
|
+
isPrettierFormatted = true;
|
|
111
|
+
}
|
|
112
|
+
if (isPrettierFormatted || isLintFormatted) {
|
|
113
|
+
return [fp, RESULT_STATUS.CHANGED];
|
|
114
|
+
}
|
|
115
|
+
const formattedStatus = isPrettierFormatted && isLintFormatted ? RESULT_STATUS.VALID : RESULT_STATUS.NEEDS_FORMAT;
|
|
116
|
+
return [fp, formattedStatus];
|
|
117
|
+
}));
|
|
118
|
+
return results;
|
|
119
|
+
}
|
|
120
|
+
function printStatus(lintResults, showSkipped = false) {
|
|
121
|
+
let textColor = constants_1.ANSI_COLORS.RESET_COLOR;
|
|
122
|
+
for (const [shortPath, status] of lintResults) {
|
|
123
|
+
switch (status) {
|
|
124
|
+
case RESULT_STATUS.DIRECTORY: {
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
case RESULT_STATUS.SKIPPED: {
|
|
128
|
+
if (!showSkipped) {
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
textColor = constants_1.ANSI_COLORS.GRAY_COLOR;
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
134
|
+
case RESULT_STATUS.VALID: {
|
|
135
|
+
textColor = constants_1.ANSI_COLORS.GRAY_COLOR;
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
case RESULT_STATUS.NEEDS_FORMAT: {
|
|
139
|
+
textColor = constants_1.ANSI_COLORS.YELLOW_COLOR;
|
|
140
|
+
break;
|
|
141
|
+
}
|
|
142
|
+
case RESULT_STATUS.CHANGED: {
|
|
143
|
+
textColor = constants_1.ANSI_COLORS.YELLOW_COLOR;
|
|
144
|
+
break;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
console.log(`${(0, logUtils_1.wrapInColor)(shortPath, textColor)} - ${status}`);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.wrapInColor = wrapInColor;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const Constants = tslib_1.__importStar(require("../core/constants"));
|
|
6
|
+
function wrapInColor(s, color, reset_color = Constants.ANSI_COLORS.RESET_COLOR) {
|
|
7
|
+
return `${color}${s ?? ''}${reset_color}`;
|
|
8
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.relativePathTo = relativePathTo;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const path_1 = tslib_1.__importDefault(require("path"));
|
|
6
|
+
function relativePathTo(from, to) {
|
|
7
|
+
const output = path_1.default.relative(from, to);
|
|
8
|
+
if (!output) {
|
|
9
|
+
return '.';
|
|
10
|
+
}
|
|
11
|
+
return output;
|
|
12
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.toPascalCase = toPascalCase;
|
|
4
|
+
exports.toSnakeCase = toSnakeCase;
|
|
5
|
+
const wordSplitRegex = /[\W_]/;
|
|
6
|
+
function toPascalCase(str) {
|
|
7
|
+
const words = str.trim().toLowerCase().split(wordSplitRegex);
|
|
8
|
+
return words.reduce((acc, curr) => {
|
|
9
|
+
const [firstChar, ...rest] = curr;
|
|
10
|
+
const pascalCaseWord = firstChar?.toUpperCase()?.concat(rest.join('')) ?? '';
|
|
11
|
+
return acc + pascalCaseWord;
|
|
12
|
+
}, '');
|
|
13
|
+
}
|
|
14
|
+
function toSnakeCase(str) {
|
|
15
|
+
const words = str.trim().toLowerCase().split(wordSplitRegex);
|
|
16
|
+
return words.join('_');
|
|
17
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
require("jasmine");
|
|
4
|
+
const stringUtils_1 = require("./stringUtils");
|
|
5
|
+
describe('stringUtils', () => {
|
|
6
|
+
it('converts strings to pascal case', () => {
|
|
7
|
+
const testCases = [['hello', 'Hello'], ['hello world', 'HelloWorld'], ['My New Module', 'MyNewModule'], ['my_new_module', 'MyNewModule']];
|
|
8
|
+
testCases.forEach(([input, expected]) => {
|
|
9
|
+
expect((0, stringUtils_1.toPascalCase)(input)).toBe(expected);
|
|
10
|
+
});
|
|
11
|
+
});
|
|
12
|
+
it('converts strings to snake case', () => {
|
|
13
|
+
const testCases = [['hello', 'hello'], ['hello world', 'hello_world']];
|
|
14
|
+
testCases.forEach(([input, expected]) => {
|
|
15
|
+
expect((0, stringUtils_1.toSnakeCase)(input)).toBe(expected);
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.makeTempDir = makeTempDir;
|
|
4
|
+
exports.withTempDir = withTempDir;
|
|
5
|
+
const tslib_1 = require("tslib");
|
|
6
|
+
const crypto_1 = tslib_1.__importDefault(require("crypto"));
|
|
7
|
+
const fs = tslib_1.__importStar(require("node:fs"));
|
|
8
|
+
const os_1 = tslib_1.__importDefault(require("os"));
|
|
9
|
+
const path_1 = tslib_1.__importDefault(require("path"));
|
|
10
|
+
function makeTempDir() {
|
|
11
|
+
return path_1.default.join(os_1.default.tmpdir(), crypto_1.default.randomUUID());
|
|
12
|
+
}
|
|
13
|
+
async function withTempDir(receiveTempDir) {
|
|
14
|
+
const tempDir = makeTempDir();
|
|
15
|
+
fs.mkdirSync(tempDir);
|
|
16
|
+
try {
|
|
17
|
+
return await receiveTempDir(tempDir);
|
|
18
|
+
}
|
|
19
|
+
finally {
|
|
20
|
+
try {
|
|
21
|
+
fs.rmdirSync(tempDir);
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.decompressTo = decompressTo;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const node_path_1 = tslib_1.__importDefault(require("node:path"));
|
|
6
|
+
const extract_zip_1 = tslib_1.__importDefault(require("extract-zip"));
|
|
7
|
+
async function decompressTo(inputFilePath, outputFilePath) {
|
|
8
|
+
const archive = node_path_1.default.resolve(inputFilePath);
|
|
9
|
+
const destination = node_path_1.default.resolve(outputFilePath);
|
|
10
|
+
try {
|
|
11
|
+
await (0, extract_zip_1.default)(archive, { dir: destination });
|
|
12
|
+
}
|
|
13
|
+
catch (error) {
|
|
14
|
+
throw new Error(`Failed to extract “${archive}” → “${destination}”: ${error.message}`);
|
|
15
|
+
}
|
|
16
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@snap/valdi",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "Valdi CLI tools for developers",
|
|
5
|
+
"license": "ISC",
|
|
6
|
+
"bin": {
|
|
7
|
+
"valdi": "dist/index.js"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"prepare": "npm run build",
|
|
11
|
+
"build": "npm exec --package=typescript@5.9.2 tsc -- --project ./tsconfig.dist.json",
|
|
12
|
+
"test": "ts-node node_modules/jasmine/bin/jasmine",
|
|
13
|
+
"watch": "tsc --watch",
|
|
14
|
+
"main": "ts-node ./src/index.ts",
|
|
15
|
+
"format": "prettier --ignore-path .gitignore --write \"**/*.+(js|ts|json)\"",
|
|
16
|
+
"eslint": "eslint './{src,test}/**/*.{ts,tsx}'",
|
|
17
|
+
"eslint:fix": "npm run eslint -- --fix",
|
|
18
|
+
"cli:install": "npm ci && npm link",
|
|
19
|
+
"cli:uninstall": "PKG_NAME=$(npm pkg get name | tr -d '\"') && npm uninstall $PKG_NAME -g",
|
|
20
|
+
"cli:uninstall_all": "PKG_NAME=$(npm pkg get name | tr -d '\"') && npm uninstall $PKG_NAME && npm uninstall -g $PKG_NAME"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@bazel/bazelisk": "^1.19.0",
|
|
24
|
+
"@types/inquirer": "^9.0.7",
|
|
25
|
+
"@types/yargs": "^17.0.32",
|
|
26
|
+
"@typescript-eslint/eslint-plugin": "^5.59.9",
|
|
27
|
+
"eslint": "^8.42.0",
|
|
28
|
+
"eslint-config-prettier": "^8.8.0",
|
|
29
|
+
"eslint-plugin-import": "^2.27.5",
|
|
30
|
+
"eslint-plugin-unicorn": "^47.0.0",
|
|
31
|
+
"extract-zip": "^2.0.1",
|
|
32
|
+
"glob": "^11.0.1",
|
|
33
|
+
"inquirer": "^8.0.0",
|
|
34
|
+
"jsonc-parser": "^3.3.1",
|
|
35
|
+
"ora": "^8.2.0",
|
|
36
|
+
"prettier": "^3.3.1",
|
|
37
|
+
"tslib": "^2.5.3",
|
|
38
|
+
"typescript": "^5.3.3",
|
|
39
|
+
"yaml": "^2.4.5",
|
|
40
|
+
"yargs": "^17.7.2"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@types/eslint": "^9.6.1",
|
|
44
|
+
"@types/jasmine": "^5.1.7",
|
|
45
|
+
"jasmine": "^5.6.0",
|
|
46
|
+
"ts-node": "^10.9.2"
|
|
47
|
+
},
|
|
48
|
+
"overrides": {
|
|
49
|
+
"brace-expansion": "2.0.2"
|
|
50
|
+
}
|
|
51
|
+
}
|