@blastlauncher/cli 1.0.4 → 3.0.0-alpha.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/index.js DELETED
@@ -1,265 +0,0 @@
1
- #!/usr/bin/env node
2
- import { execSync } from "child_process";
3
- import https from "https";
4
- import path from "path";
5
-
6
- import { Command } from "commander";
7
- import esbuild from "esbuild";
8
- import fs from "fs-extra";
9
- import semver from "semver";
10
- import { temporaryFile } from "tempy";
11
-
12
- function getJSON(url) {
13
- return new Promise((resolve, reject) => {
14
- https
15
- .get(url, (res) => {
16
- let body = "";
17
- res.on("data", (chunk) => {
18
- body += chunk;
19
- });
20
- res.on("end", () => {
21
- try {
22
- resolve(JSON.parse(body));
23
- } catch (e) {
24
- reject(e);
25
- }
26
- });
27
- })
28
- .on("error", (e) => {
29
- reject(e);
30
- });
31
- });
32
- }
33
-
34
- class PackageVersionHelper {
35
- constructor(npmOrg, registryUrl = "https://registry.npmjs.org") {
36
- this.npmOrg = npmOrg;
37
- this.registryUrl = registryUrl;
38
- }
39
-
40
- async fetchPackageVersions(packageName) {
41
- let res;
42
- try {
43
- console.log(`fetching ${this.registryUrl}/@${this.npmOrg}/${packageName}`);
44
- res = await getJSON(`${this.registryUrl}/@${this.npmOrg}/${packageName}`);
45
-
46
- if (res.error) {
47
- return null;
48
- }
49
-
50
- return Object.keys(res.versions);
51
- } catch (e) {
52
- // pacakge not found
53
- return null;
54
- }
55
- }
56
-
57
- async getLatestVersion(versions) {
58
- const sortedVersions = versions.sort((a, b) => semver.compare(a, b));
59
- return sortedVersions[sortedVersions.length - 1];
60
- }
61
-
62
- async getPackageVersion(packageName) {
63
- const versions = await this.fetchPackageVersions(packageName);
64
- if (!versions) {
65
- return null;
66
- }
67
-
68
- return this.getLatestVersion(versions);
69
- }
70
-
71
- getPackageName(extensionFolder) {
72
- const packageJson = JSON.parse(fs.readFileSync(path.resolve(extensionFolder, "package.json"), "utf8"));
73
- let packageName = packageJson.name;
74
- if (packageName.startsWith("@")) {
75
- packageName = packageName.split("/")[1];
76
- }
77
- packageName = packageName.toLowerCase();
78
-
79
- console.debug(`package name is ${packageName}`);
80
-
81
- return packageName;
82
- }
83
- }
84
-
85
- /**
86
- * @param {string} extensionFolder
87
- * @param {string} npmOrganization
88
- * @param {string} npmRegistry
89
- * @param {string} distDir
90
- */
91
- async function publishExtensions(extensionFolder, npmOrganization, npmRegistry, distDir) {
92
- const versionHelper = new PackageVersionHelper(npmOrganization, npmRegistry);
93
-
94
- console.log(`\nEntering ${extensionFolder}\n`);
95
- execSync(`cd "${extensionFolder}"`);
96
-
97
- if (!distDir) {
98
- distDir = path.resolve(extensionFolder, "dist");
99
- }
100
-
101
- if (!fs.existsSync(distDir)) {
102
- fs.mkdirSync(distDir);
103
- }
104
-
105
- await buildExtension(extensionFolder, distDir);
106
-
107
- const packageName = versionHelper.getPackageName(extensionFolder);
108
- let version = await versionHelper.getPackageVersion(packageName);
109
- if (version) {
110
- console.info(`current version is ${version}`);
111
- version = semver.inc(version, "major");
112
- } else {
113
- console.info(`no version found, using 1.0.0`);
114
- version = "1.0.0";
115
- }
116
-
117
- updatePackageInfo(distDir, version, npmOrganization);
118
-
119
- console.info(`publishing version ${version}`);
120
-
121
- // publish to npm
122
- execSync(`npm publish --access public`, {
123
- cwd: distDir,
124
- });
125
- }
126
-
127
- function getTSConfigPath(extensionDir) {
128
- const originalTSconfigPath = path.resolve(extensionDir, "tsconfig.json");
129
- const tsconfigJson = JSON.parse(fs.readFileSync(originalTSconfigPath, "utf8"));
130
-
131
- tsconfigJson.compilerOptions.jsx = "react";
132
- tsconfigJson.compilerOptions.strict = false;
133
-
134
- const tempTSconfigPath = temporaryFile({ extension: "json" });
135
-
136
- fs.writeFileSync(tempTSconfigPath, JSON.stringify(tsconfigJson, null, 2));
137
-
138
- return tempTSconfigPath;
139
- }
140
-
141
- async function buildExtension(extensionDir, outputFolder) {
142
- // Gather entry points from target extension's packages.json
143
- const packageJson = JSON.parse(fs.readFileSync(path.resolve(extensionDir, "package.json"), "utf8"));
144
- if (!outputFolder) {
145
- outputFolder = path.resolve(extensionDir, "dist");
146
- }
147
-
148
- const commandNames = packageJson.commands.map((command) => command.name);
149
-
150
- const supportedExts = [".js", ".ts", ".tsx", ".jsx"];
151
-
152
- const tsconfigPath = getTSConfigPath(extensionDir);
153
-
154
- const esbuildConfigs = commandNames.map((commandName) => {
155
- const files = fs.readdirSync(path.join(extensionDir, "src"));
156
- const entry = files.find((file) => {
157
- return supportedExts.some((ext) => file === `${commandName}${ext}`);
158
- });
159
-
160
- if (!entry) {
161
- throw new Error(`No entry point found for ${commandName}`);
162
- }
163
-
164
- const entryPath = path.join(extensionDir, "src", entry);
165
- const outputPath = path.join(outputFolder, `${commandName}.js`);
166
-
167
- console.debug(`entry point for ${commandName} is ${entryPath}`);
168
-
169
- return {
170
- input: entryPath,
171
- output: outputPath,
172
- };
173
- });
174
-
175
- console.info(`entry points [${esbuildConfigs.map((config) => config.input).join(" ")}]`);
176
-
177
- // Build each entry point
178
- for (const config of esbuildConfigs) {
179
- await esbuild.build({
180
- entryPoints: [config.input],
181
- bundle: true,
182
- platform: "node",
183
- outfile: config.output,
184
- external: ["@raycast/api", "react"],
185
- jsx: "transform",
186
- jsxFactory: "_jsx",
187
- jsxFragment: "_jsxFragment",
188
- tsconfig: tsconfigPath,
189
- minify: true,
190
- });
191
- }
192
-
193
- // copy the package.json to the output folder
194
- fs.copyFileSync(path.join(extensionDir, "package.json"), path.join(outputFolder, "package.json"));
195
-
196
- // copy assets directory to the output folder
197
- const assetsDir = path.join(extensionDir, "assets");
198
- if (fs.existsSync(assetsDir)) {
199
- fs.copySync(assetsDir, path.join(outputFolder, "assets"));
200
- }
201
- }
202
-
203
- /**
204
- * @param {string} targetDir
205
- * @param {string} version
206
- * @param {string} npmOrganization
207
- * @returns {void}
208
- * add extra information to package.json
209
- **/
210
- function updatePackageInfo(targetDir, version, npmOrganization) {
211
- const packagePath = path.join(targetDir, "package.json");
212
- const packageJson = JSON.parse(fs.readFileSync(packagePath, "utf8"));
213
-
214
- // prepend @BlastLauncher to name if it's not already there
215
- if (!packageJson.name.startsWith(`@${npmOrganization}`)) {
216
- packageJson.name = `@${npmOrganization}/${packageJson.name}`;
217
- }
218
-
219
- // add repository field
220
- packageJson.repository = "https://github.com/BlastLauncher/extensions.git";
221
-
222
- // add version
223
- packageJson.version = version;
224
-
225
- // exclude original publish script
226
- packageJson.scripts = Object.keys(packageJson.scripts)
227
- .filter((key) => key !== "publish")
228
- .reduce((obj, key) => {
229
- obj[key] = packageJson.scripts[key];
230
- return obj;
231
- }, {});
232
-
233
- // write package.json
234
- fs.writeFileSync(packagePath, JSON.stringify(packageJson, null, 2), "utf8");
235
- }
236
-
237
- const program = new Command();
238
-
239
- program.name("blast-cli").description(`CLI for Blast Launcher
240
-
241
- Example Usage:
242
- blast build ./extensions/todo-list -o ./extensions/todo-list/dist
243
- blast publish ./extensions/todo-list blast-extensions`);
244
-
245
- program
246
- .command("publish")
247
- .description("Publish extensions")
248
- .argument("<path>", "Path to extensions folder")
249
- .argument("<organization>", "NPM organization to publish to, e.g. blast-extensions")
250
- .option("-r, --registry <registry>", "NPM registry to publish to", "https://registry.npmjs.org")
251
- .option("-o, --output <output>", "Output directory, default to ./dist folder relative to extension path")
252
- .action((dir, organization, options) => {
253
- return publishExtensions(dir, organization, options.registry, options.output);
254
- });
255
-
256
- program
257
- .command("build")
258
- .description("Build extensions")
259
- .argument("<path>", "Path to extension")
260
- .option("-o, --output <output>", "Output directory, default to ./dist folder relative to extension path")
261
- .action((path, options) => {
262
- return buildExtension(path, options.output);
263
- });
264
-
265
- program.parse();