@konomi-app/k2 3.4.1 → 4.1.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/dist/index.js +364 -348
- package/dist/index.js.map +1 -1
- package/dist/plugin.js +583 -585
- package/dist/plugin.js.map +1 -1
- package/package.json +37 -35
package/dist/index.js
CHANGED
|
@@ -1,19 +1,12 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
3
|
-
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
4
|
-
}) : x)(function(x) {
|
|
5
|
-
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
6
|
-
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
7
|
-
});
|
|
8
2
|
|
|
9
3
|
// src/index.ts
|
|
10
|
-
import { program as
|
|
4
|
+
import { program as program5 } from "commander";
|
|
11
5
|
|
|
12
6
|
// src/commands/build.ts
|
|
13
7
|
import { program } from "commander";
|
|
14
|
-
import "fs-extra";
|
|
15
8
|
import path6 from "path";
|
|
16
|
-
import
|
|
9
|
+
import chalk from "chalk";
|
|
17
10
|
|
|
18
11
|
// src/lib/constants.ts
|
|
19
12
|
import path from "path";
|
|
@@ -143,198 +136,143 @@ var getDefaultK2Config = () => {
|
|
|
143
136
|
};
|
|
144
137
|
};
|
|
145
138
|
|
|
146
|
-
// src/lib/
|
|
147
|
-
import
|
|
148
|
-
import
|
|
149
|
-
import os from "os";
|
|
139
|
+
// src/lib/rsbuild.ts
|
|
140
|
+
import { createRsbuild } from "@rsbuild/core";
|
|
141
|
+
import { pluginReact } from "@rsbuild/plugin-react";
|
|
150
142
|
import path5 from "path";
|
|
151
|
-
import
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
143
|
+
import fs2 from "fs-extra";
|
|
144
|
+
function getRsbuildPlugins() {
|
|
145
|
+
return [
|
|
146
|
+
pluginReact({
|
|
147
|
+
swcReactOptions: {
|
|
148
|
+
runtime: "automatic"
|
|
149
|
+
}
|
|
150
|
+
})
|
|
151
|
+
];
|
|
152
|
+
}
|
|
153
|
+
async function buildWithRsbuild(params) {
|
|
154
|
+
const { entries, outDir, minify = true, sourcemap = false, injectStyles = true } = params;
|
|
155
|
+
const sourceMapConfig = sourcemap === "inline" ? "cheap-module-source-map" : sourcemap ? "source-map" : false;
|
|
156
|
+
const rsbuild = await createRsbuild({
|
|
157
|
+
rsbuildConfig: {
|
|
158
|
+
plugins: getRsbuildPlugins(),
|
|
159
|
+
source: { entry: entries },
|
|
160
|
+
output: {
|
|
161
|
+
target: "web",
|
|
162
|
+
distPath: { root: outDir, js: "" },
|
|
163
|
+
filename: { js: "[name].js" },
|
|
164
|
+
filenameHash: false,
|
|
165
|
+
cleanDistPath: true,
|
|
166
|
+
injectStyles,
|
|
167
|
+
sourceMap: { js: sourceMapConfig },
|
|
168
|
+
minify
|
|
169
|
+
},
|
|
170
|
+
performance: {
|
|
171
|
+
chunkSplit: { strategy: "all-in-one" }
|
|
172
|
+
},
|
|
173
|
+
tools: {
|
|
174
|
+
htmlPlugin: false
|
|
175
|
+
}
|
|
166
176
|
}
|
|
167
|
-
}
|
|
168
|
-
await
|
|
169
|
-
return results;
|
|
177
|
+
});
|
|
178
|
+
await rsbuild.build();
|
|
170
179
|
}
|
|
171
|
-
|
|
172
|
-
const
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
define: {
|
|
186
|
-
"process.env.NODE_ENV": JSON.stringify(mode),
|
|
187
|
-
"process.env": JSON.stringify({}),
|
|
188
|
-
...config.define
|
|
180
|
+
async function startRsbuildDevServer(params) {
|
|
181
|
+
const { entries, outDir, port, https, publicDir, onFirstCompile, onRecompile } = params;
|
|
182
|
+
const rsbuildConfig = {
|
|
183
|
+
plugins: getRsbuildPlugins(),
|
|
184
|
+
source: { entry: entries },
|
|
185
|
+
output: {
|
|
186
|
+
target: "web",
|
|
187
|
+
distPath: { root: outDir, js: "" },
|
|
188
|
+
filename: { js: "[name].js" },
|
|
189
|
+
filenameHash: false,
|
|
190
|
+
cleanDistPath: false,
|
|
191
|
+
injectStyles: true,
|
|
192
|
+
sourceMap: { js: "cheap-module-source-map" },
|
|
193
|
+
minify: false
|
|
189
194
|
},
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
// ブラウザ向けターゲット
|
|
193
|
-
target: config.build?.target ?? "es2020",
|
|
194
|
-
cssCodeSplit: false,
|
|
195
|
-
rollupOptions: {
|
|
196
|
-
...config.build?.rollupOptions,
|
|
197
|
-
onwarn: (warning, warn) => {
|
|
198
|
-
if (["MODULE_LEVEL_DIRECTIVE"].includes(warning.code ?? "")) {
|
|
199
|
-
return;
|
|
200
|
-
}
|
|
201
|
-
if (warning.message?.includes("sourcemap") || warning.code === "SOURCEMAP_ERROR") {
|
|
202
|
-
return;
|
|
203
|
-
}
|
|
204
|
-
warn(warning);
|
|
205
|
-
}
|
|
206
|
-
}
|
|
195
|
+
performance: {
|
|
196
|
+
chunkSplit: { strategy: "all-in-one" }
|
|
207
197
|
},
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
}
|
|
198
|
+
tools: {
|
|
199
|
+
htmlPlugin: false
|
|
200
|
+
},
|
|
201
|
+
server: {
|
|
202
|
+
port,
|
|
203
|
+
host: "0.0.0.0",
|
|
204
|
+
...https ? { https } : {},
|
|
205
|
+
...publicDir && fs2.existsSync(publicDir) ? { publicDir: { name: publicDir } } : {}
|
|
206
|
+
},
|
|
207
|
+
dev: {
|
|
208
|
+
writeToDisk: true
|
|
215
209
|
}
|
|
216
210
|
};
|
|
217
|
-
};
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
const name = entryNames[0];
|
|
231
|
-
await buildSingleEntry({
|
|
232
|
-
name,
|
|
233
|
-
entryPath: entries[name],
|
|
234
|
-
outDir,
|
|
235
|
-
mode,
|
|
236
|
-
sourcemap,
|
|
237
|
-
minify,
|
|
238
|
-
viteConfig
|
|
239
|
-
});
|
|
240
|
-
return;
|
|
241
|
-
}
|
|
242
|
-
await parallelLimit(entryNames, concurrency, async (name) => {
|
|
243
|
-
await buildSingleEntry({
|
|
244
|
-
name,
|
|
245
|
-
entryPath: entries[name],
|
|
246
|
-
outDir,
|
|
247
|
-
mode,
|
|
248
|
-
sourcemap,
|
|
249
|
-
minify,
|
|
250
|
-
viteConfig
|
|
251
|
-
});
|
|
252
|
-
});
|
|
253
|
-
}
|
|
254
|
-
async function buildSingleEntry(params) {
|
|
255
|
-
const { name, entryPath, outDir, mode, sourcemap, minify, viteConfig } = params;
|
|
256
|
-
const config = createViteConfig({
|
|
257
|
-
...viteConfig,
|
|
258
|
-
mode,
|
|
259
|
-
build: {
|
|
260
|
-
...viteConfig.build,
|
|
261
|
-
rollupOptions: {
|
|
262
|
-
...viteConfig.build?.rollupOptions,
|
|
263
|
-
input: entryPath,
|
|
264
|
-
output: {
|
|
265
|
-
entryFileNames: `${name}.js`,
|
|
266
|
-
assetFileNames: `${name}.[ext]`,
|
|
267
|
-
format: "iife",
|
|
268
|
-
// 外部ライブラリのソースマップを無視して警告を抑制
|
|
269
|
-
sourcemapIgnoreList: (relativeSourcePath) => {
|
|
270
|
-
return relativeSourcePath.includes("node_modules");
|
|
271
|
-
},
|
|
272
|
-
...viteConfig.build?.rollupOptions?.output
|
|
211
|
+
const rsbuild = await createRsbuild({ rsbuildConfig });
|
|
212
|
+
if (onFirstCompile || onRecompile) {
|
|
213
|
+
rsbuild.addPlugins([
|
|
214
|
+
{
|
|
215
|
+
name: "k2-dev-hooks",
|
|
216
|
+
setup(api) {
|
|
217
|
+
api.onDevCompileDone(async ({ isFirstCompile }) => {
|
|
218
|
+
if (isFirstCompile && onFirstCompile) {
|
|
219
|
+
await onFirstCompile();
|
|
220
|
+
} else if (!isFirstCompile && onRecompile) {
|
|
221
|
+
await onRecompile();
|
|
222
|
+
}
|
|
223
|
+
});
|
|
273
224
|
}
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
emptyOutDir: false,
|
|
277
|
-
sourcemap,
|
|
278
|
-
minify,
|
|
279
|
-
cssCodeSplit: false
|
|
280
|
-
}
|
|
281
|
-
});
|
|
282
|
-
await viteBuild(config);
|
|
283
|
-
const outputPath = path5.join(outDir, `${name}.js`);
|
|
284
|
-
if (fs2.existsSync(outputPath)) {
|
|
285
|
-
const content = await fs2.readFile(outputPath, "utf-8");
|
|
286
|
-
const wrapped = `(function(){
|
|
287
|
-
${content}
|
|
288
|
-
})();`;
|
|
289
|
-
await fs2.writeFile(outputPath, wrapped);
|
|
225
|
+
}
|
|
226
|
+
]);
|
|
290
227
|
}
|
|
228
|
+
const result = await rsbuild.startDevServer();
|
|
229
|
+
return {
|
|
230
|
+
port: result.port,
|
|
231
|
+
close: result.server.close
|
|
232
|
+
};
|
|
291
233
|
}
|
|
292
|
-
|
|
293
|
-
function getEntryPointsFromDir(inputDir) {
|
|
294
|
-
const entries = {};
|
|
234
|
+
function getAppEntryPoints(inputDir) {
|
|
295
235
|
if (!fs2.existsSync(inputDir)) {
|
|
296
|
-
|
|
236
|
+
return {};
|
|
297
237
|
}
|
|
298
|
-
const
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
const
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
entries[dirName] = entryPath;
|
|
307
|
-
break;
|
|
238
|
+
const allProjects = fs2.readdirSync(inputDir);
|
|
239
|
+
return allProjects.reduce((acc, dir) => {
|
|
240
|
+
const dirPath = path5.join(inputDir, dir);
|
|
241
|
+
if (!fs2.statSync(dirPath).isDirectory()) return acc;
|
|
242
|
+
for (const filename of ["index.ts", "index.tsx", "index.js", "index.jsx", "index.mjs"]) {
|
|
243
|
+
const filePath = path5.join(inputDir, dir, filename);
|
|
244
|
+
if (fs2.existsSync(filePath)) {
|
|
245
|
+
return { ...acc, [dir]: filePath };
|
|
308
246
|
}
|
|
309
247
|
}
|
|
310
|
-
|
|
311
|
-
|
|
248
|
+
return acc;
|
|
249
|
+
}, {});
|
|
312
250
|
}
|
|
313
251
|
|
|
314
252
|
// src/commands/build.ts
|
|
315
253
|
function command() {
|
|
316
|
-
program.command("build").option("-o, --outdir <outdir>", "Output directory.", path6.join(WORKSPACE_DIRECTORY, "prod")).option("-i, --input <input>", "Input directory.", path6.join("src", "apps")).option("--config <config>", "k2 config file path").description("Build the project for production with
|
|
254
|
+
program.command("build").option("-o, --outdir <outdir>", "Output directory.", path6.join(WORKSPACE_DIRECTORY, "prod")).option("-i, --input <input>", "Input directory.", path6.join("src", "apps")).option("--config <config>", "k2 config file path").description("Build the project for production with rsbuild.").action(action);
|
|
317
255
|
}
|
|
318
256
|
async function action(options) {
|
|
319
257
|
console.group("\u{1F373} Build the project for production");
|
|
320
258
|
try {
|
|
321
259
|
const { outdir, input, config } = options;
|
|
322
260
|
const outDir = path6.resolve(outdir);
|
|
323
|
-
const entries =
|
|
261
|
+
const entries = getAppEntryPoints(path6.resolve(input));
|
|
324
262
|
const entryNames = Object.keys(entries);
|
|
325
263
|
if (entryNames.length === 0) {
|
|
326
264
|
throw new Error(`No entry points found in ${input}`);
|
|
327
265
|
}
|
|
328
|
-
console.log(
|
|
266
|
+
console.log(chalk.gray(` Entry points: ${entryNames.join(", ")}`));
|
|
329
267
|
const k2Config = config ? await importK2Config(config) : getDefaultK2Config();
|
|
330
268
|
const fullConfig = { ...k2Config, outDir };
|
|
331
269
|
const results = await Promise.allSettled([
|
|
332
|
-
|
|
270
|
+
buildWithRsbuild({
|
|
333
271
|
entries,
|
|
334
272
|
outDir,
|
|
335
|
-
|
|
273
|
+
minify: true,
|
|
336
274
|
sourcemap: false,
|
|
337
|
-
|
|
275
|
+
injectStyles: true
|
|
338
276
|
}),
|
|
339
277
|
buildTailwind(fullConfig)
|
|
340
278
|
]);
|
|
@@ -353,15 +291,14 @@ async function action(options) {
|
|
|
353
291
|
|
|
354
292
|
// src/commands/dev/index.ts
|
|
355
293
|
import { program as program2 } from "commander";
|
|
356
|
-
import
|
|
357
|
-
import
|
|
358
|
-
import
|
|
359
|
-
import chalk4 from "chalk";
|
|
294
|
+
import "fs-extra";
|
|
295
|
+
import path10 from "path";
|
|
296
|
+
import chalk3 from "chalk";
|
|
360
297
|
|
|
361
298
|
// src/commands/dev/tailwind.ts
|
|
362
299
|
import path7 from "path";
|
|
363
|
-
import
|
|
364
|
-
import
|
|
300
|
+
import chalk2 from "chalk";
|
|
301
|
+
import fs3 from "fs-extra";
|
|
365
302
|
var watchCss = async (params) => {
|
|
366
303
|
const { k2Config, outdir } = params;
|
|
367
304
|
if (!k2Config.tailwind?.css || !k2Config.tailwind?.config) {
|
|
@@ -371,8 +308,8 @@ var watchCss = async (params) => {
|
|
|
371
308
|
const tailwindConfig = await getTailwindConfigFromK2Config(k2Config.tailwind);
|
|
372
309
|
const input = path7.resolve(k2Config.tailwind.css);
|
|
373
310
|
const output = path7.join(outdir, "tailwind.css");
|
|
374
|
-
if (!await
|
|
375
|
-
await
|
|
311
|
+
if (!await fs3.pathExists(output)) {
|
|
312
|
+
await fs3.outputFile(output, "");
|
|
376
313
|
}
|
|
377
314
|
return watchTailwindCSS({
|
|
378
315
|
input,
|
|
@@ -381,50 +318,239 @@ var watchCss = async (params) => {
|
|
|
381
318
|
onChanges: ({ output: output2, type }) => {
|
|
382
319
|
const outputFileName = path7.basename(output2);
|
|
383
320
|
console.log(
|
|
384
|
-
|
|
321
|
+
chalk2.hex("#e5e7eb")(`${(/* @__PURE__ */ new Date()).toLocaleTimeString()} `) + chalk2.cyan(`[css] `) + outputFileName + (type === "init" ? " init" : ` rebuilt(${type})`)
|
|
385
322
|
);
|
|
386
323
|
}
|
|
387
324
|
});
|
|
388
325
|
};
|
|
389
326
|
|
|
390
|
-
// src/lib/
|
|
391
|
-
import
|
|
392
|
-
import
|
|
393
|
-
var exec = promisify(defaultExec);
|
|
327
|
+
// src/lib/cert/index.ts
|
|
328
|
+
import fs4 from "fs-extra";
|
|
329
|
+
import path9 from "path";
|
|
394
330
|
|
|
395
|
-
// src/lib/cert.ts
|
|
396
|
-
import
|
|
331
|
+
// src/lib/cert/cert.ts
|
|
332
|
+
import forge from "node-forge";
|
|
333
|
+
import net from "net";
|
|
334
|
+
var { md, pki } = forge;
|
|
335
|
+
function createCertificate(serial, publicKey, subject, issuer, extensions, validity, signWith) {
|
|
336
|
+
const cert = pki.createCertificate();
|
|
337
|
+
cert.serialNumber = Buffer.from(serial).toString("hex");
|
|
338
|
+
cert.publicKey = publicKey;
|
|
339
|
+
cert.setSubject(subject);
|
|
340
|
+
cert.setIssuer(issuer);
|
|
341
|
+
cert.setExtensions(extensions);
|
|
342
|
+
cert.validity.notBefore = /* @__PURE__ */ new Date();
|
|
343
|
+
cert.validity.notAfter = /* @__PURE__ */ new Date();
|
|
344
|
+
cert.validity.notAfter.setDate(cert.validity.notAfter.getDate() + validity);
|
|
345
|
+
cert.sign(signWith, md.sha256.create());
|
|
346
|
+
return cert;
|
|
347
|
+
}
|
|
348
|
+
function generateCertInternal(params) {
|
|
349
|
+
const { subject, issuer, extensions, validity, signWith } = params;
|
|
350
|
+
const serial = Math.floor(Math.random() * 95e3 + 5e4).toString();
|
|
351
|
+
const keyPair = pki.rsa.generateKeyPair(2048);
|
|
352
|
+
const privateKey = signWith ? pki.privateKeyFromPem(signWith) : keyPair.privateKey;
|
|
353
|
+
const cert = createCertificate(
|
|
354
|
+
serial,
|
|
355
|
+
keyPair.publicKey,
|
|
356
|
+
subject,
|
|
357
|
+
issuer,
|
|
358
|
+
extensions,
|
|
359
|
+
validity,
|
|
360
|
+
privateKey
|
|
361
|
+
);
|
|
362
|
+
return {
|
|
363
|
+
key: pki.privateKeyToPem(keyPair.privateKey),
|
|
364
|
+
cert: pki.certificateToPem(cert)
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
function createCA(params = {}) {
|
|
368
|
+
const {
|
|
369
|
+
organization = "K2 Development CA",
|
|
370
|
+
countryCode = "JP",
|
|
371
|
+
state = "Tokyo",
|
|
372
|
+
locality = "Tokyo",
|
|
373
|
+
validity = 7300
|
|
374
|
+
} = params;
|
|
375
|
+
const attributes = [
|
|
376
|
+
{ name: "commonName", value: organization },
|
|
377
|
+
{ name: "countryName", value: countryCode },
|
|
378
|
+
{ name: "stateOrProvinceName", value: state },
|
|
379
|
+
{ name: "localityName", value: locality },
|
|
380
|
+
{ name: "organizationName", value: organization }
|
|
381
|
+
];
|
|
382
|
+
const extensions = [
|
|
383
|
+
{ name: "basicConstraints", cA: true, critical: true },
|
|
384
|
+
{ name: "keyUsage", keyCertSign: true, critical: true }
|
|
385
|
+
];
|
|
386
|
+
return generateCertInternal({
|
|
387
|
+
subject: attributes,
|
|
388
|
+
issuer: attributes,
|
|
389
|
+
extensions,
|
|
390
|
+
validity
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
function createCert(params) {
|
|
394
|
+
const { ca, domains, validity = 7300 } = params;
|
|
395
|
+
const attributes = [{ name: "commonName", value: domains[0] }];
|
|
396
|
+
const extensions = [
|
|
397
|
+
{ name: "basicConstraints", cA: false, critical: true },
|
|
398
|
+
{
|
|
399
|
+
name: "keyUsage",
|
|
400
|
+
digitalSignature: true,
|
|
401
|
+
keyEncipherment: true,
|
|
402
|
+
critical: true
|
|
403
|
+
},
|
|
404
|
+
{ name: "extKeyUsage", serverAuth: true, clientAuth: true },
|
|
405
|
+
{
|
|
406
|
+
name: "subjectAltName",
|
|
407
|
+
altNames: domains.map((domain) => {
|
|
408
|
+
const TYPE_DOMAIN = 2;
|
|
409
|
+
const TYPE_IP = 7;
|
|
410
|
+
return net.isIP(domain) ? { type: TYPE_IP, ip: domain } : { type: TYPE_DOMAIN, value: domain };
|
|
411
|
+
})
|
|
412
|
+
}
|
|
413
|
+
];
|
|
414
|
+
const caCert = pki.certificateFromPem(ca.cert.toString());
|
|
415
|
+
return generateCertInternal({
|
|
416
|
+
subject: attributes,
|
|
417
|
+
issuer: caCert.subject.attributes,
|
|
418
|
+
extensions,
|
|
419
|
+
validity,
|
|
420
|
+
signWith: ca.key.toString()
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
// src/lib/cert/constants.ts
|
|
397
425
|
import path8 from "path";
|
|
426
|
+
var PKG_NAME = "k2";
|
|
427
|
+
var isSupported = process.platform === "darwin" || process.platform === "linux" || process.platform === "win32";
|
|
428
|
+
function getApplicationConfigPath(name) {
|
|
429
|
+
if (process.platform === "darwin") {
|
|
430
|
+
return path8.join(process.env.HOME, "Library", "Application Support", name);
|
|
431
|
+
}
|
|
432
|
+
if (process.platform === "win32") {
|
|
433
|
+
return process.env.LOCALAPPDATA ? path8.join(process.env.LOCALAPPDATA, name) : path8.join(process.env.USERPROFILE, "Local Settings", "Application Data", name);
|
|
434
|
+
}
|
|
435
|
+
return process.env.XDG_CONFIG_HOME ? path8.join(process.env.XDG_CONFIG_HOME, name) : path8.join(process.env.HOME, ".config", name);
|
|
436
|
+
}
|
|
437
|
+
var pkgDir = getApplicationConfigPath(PKG_NAME);
|
|
438
|
+
var rootCAPath = path8.resolve(pkgDir, "rootCA.pem");
|
|
439
|
+
var rootCAKeyPath = path8.resolve(pkgDir, "rootCA-key.pem");
|
|
440
|
+
|
|
441
|
+
// src/lib/cert/platforms.ts
|
|
442
|
+
import { spawnSync } from "child_process";
|
|
443
|
+
|
|
444
|
+
// src/lib/logger.ts
|
|
445
|
+
var PKG_NAME2 = "@konomi-app/k2";
|
|
446
|
+
function createLogger(prefix = PKG_NAME2) {
|
|
447
|
+
return {
|
|
448
|
+
log: (...args) => console.log(`[${prefix}]`, ...args),
|
|
449
|
+
info: (...args) => console.info(`[${prefix}]`, ...args),
|
|
450
|
+
warn: (...args) => console.warn(`[${prefix}]`, ...args),
|
|
451
|
+
error: (...args) => console.error(`[${prefix}]`, ...args)
|
|
452
|
+
};
|
|
453
|
+
}
|
|
454
|
+
var logger_default = createLogger();
|
|
455
|
+
|
|
456
|
+
// src/lib/cert/platforms.ts
|
|
457
|
+
function addToTrustStoresDarwin(certPath) {
|
|
458
|
+
logger_default.log("Adding certificate to trusted store. Admin rights may be required.");
|
|
459
|
+
spawnSync(
|
|
460
|
+
"sudo",
|
|
461
|
+
[
|
|
462
|
+
"security",
|
|
463
|
+
"add-trusted-cert",
|
|
464
|
+
"-d",
|
|
465
|
+
"-r",
|
|
466
|
+
"trustRoot",
|
|
467
|
+
"-k",
|
|
468
|
+
"/Library/Keychains/System.keychain",
|
|
469
|
+
"-p",
|
|
470
|
+
"ssl",
|
|
471
|
+
"-p",
|
|
472
|
+
"basic",
|
|
473
|
+
certPath
|
|
474
|
+
],
|
|
475
|
+
{ stdio: "inherit" }
|
|
476
|
+
);
|
|
477
|
+
}
|
|
478
|
+
var targetCA = `/usr/local/share/ca-certificates/${PKG_NAME}.crt`;
|
|
479
|
+
function addToTrustStoresLinux(certPath) {
|
|
480
|
+
logger_default.log("Adding certificate to trusted store. Admin rights may be required.");
|
|
481
|
+
spawnSync("sudo", ["cp", certPath, targetCA]);
|
|
482
|
+
spawnSync("sudo", ["update-ca-certificates"]);
|
|
483
|
+
}
|
|
484
|
+
function addToTrustStoresWin32(certPath) {
|
|
485
|
+
spawnSync("certutil", ["-addstore", "-user", "root", certPath], {
|
|
486
|
+
stdio: "inherit"
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
function addToTrustStores(certPath) {
|
|
490
|
+
switch (process.platform) {
|
|
491
|
+
case "darwin":
|
|
492
|
+
return addToTrustStoresDarwin(certPath);
|
|
493
|
+
case "linux":
|
|
494
|
+
return addToTrustStoresLinux(certPath);
|
|
495
|
+
case "win32":
|
|
496
|
+
return addToTrustStoresWin32(certPath);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
// src/lib/cert/index.ts
|
|
398
501
|
var CERT_KEY_FILENAME = "localhost-key.pem";
|
|
399
502
|
var CERT_FILENAME = "localhost-cert.pem";
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
503
|
+
function install(options = {}) {
|
|
504
|
+
const { validity = 7300 } = options;
|
|
505
|
+
if (!isSupported) {
|
|
506
|
+
throw new Error(`Platform not supported: "${process.platform}"`);
|
|
507
|
+
}
|
|
508
|
+
if (!fs4.existsSync(rootCAPath) && !fs4.existsSync(rootCAKeyPath)) {
|
|
509
|
+
const ca = createCA({ validity });
|
|
510
|
+
fs4.outputFileSync(rootCAPath, ca.cert);
|
|
511
|
+
fs4.outputFileSync(rootCAKeyPath, ca.key);
|
|
512
|
+
try {
|
|
513
|
+
addToTrustStores(rootCAPath);
|
|
514
|
+
} catch {
|
|
515
|
+
console.warn(
|
|
516
|
+
"\u26A0 Failed to add CA to system trust store. You may need to trust the certificate manually."
|
|
517
|
+
);
|
|
411
518
|
}
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
function certificateFor(requestedDomains = []) {
|
|
522
|
+
const validity = 7300;
|
|
523
|
+
install({ validity });
|
|
524
|
+
const requests = Array.isArray(requestedDomains) ? requestedDomains : [requestedDomains];
|
|
525
|
+
const domains = [
|
|
526
|
+
.../* @__PURE__ */ new Set(["localhost", "localhost.localdomain", "127.0.0.1", "0.0.0.0", "::1", ...requests])
|
|
527
|
+
];
|
|
528
|
+
const ca = {
|
|
529
|
+
cert: fs4.readFileSync(rootCAPath),
|
|
530
|
+
key: fs4.readFileSync(rootCAKeyPath)
|
|
531
|
+
};
|
|
532
|
+
return createCert({ ca, domains, validity });
|
|
533
|
+
}
|
|
534
|
+
function generateCert(outDir) {
|
|
535
|
+
fs4.ensureDirSync(outDir);
|
|
536
|
+
const result = certificateFor();
|
|
537
|
+
fs4.outputFileSync(path9.join(outDir, CERT_FILENAME), result.cert);
|
|
538
|
+
fs4.outputFileSync(path9.join(outDir, CERT_KEY_FILENAME), result.key);
|
|
539
|
+
return result;
|
|
540
|
+
}
|
|
415
541
|
function hasCertificates(certDir) {
|
|
416
|
-
return
|
|
542
|
+
return fs4.existsSync(path9.join(certDir, CERT_KEY_FILENAME)) && fs4.existsSync(path9.join(certDir, CERT_FILENAME));
|
|
417
543
|
}
|
|
418
544
|
function loadCertificates(certDir) {
|
|
419
545
|
return {
|
|
420
|
-
key:
|
|
421
|
-
cert:
|
|
546
|
+
key: fs4.readFileSync(path9.join(certDir, CERT_KEY_FILENAME)),
|
|
547
|
+
cert: fs4.readFileSync(path9.join(certDir, CERT_FILENAME))
|
|
422
548
|
};
|
|
423
549
|
}
|
|
424
550
|
|
|
425
551
|
// src/commands/dev/index.ts
|
|
426
552
|
function command2() {
|
|
427
|
-
program2.command("dev").description("Start development server with
|
|
553
|
+
program2.command("dev").description("Start development server with rsbuild.").option("-i, --input <input>", "Input directory", "src/apps").option("-o, --outdir <outdir>", "Output directory.", DEVELOPMENT_DIRECTORY).option("-c, --certdir <certdir>", "Certificate directory", WORKSPACE_DIRECTORY).option("--config <config>", "k2 config file path").option("-p, --port <port>", "Port number").action(action2);
|
|
428
554
|
}
|
|
429
555
|
async function action2(options) {
|
|
430
556
|
const { certdir, outdir, config, port: specifiedPort, input } = options;
|
|
@@ -439,100 +565,45 @@ async function action2(options) {
|
|
|
439
565
|
console.log(`\u2699 ${CONFIG_FILE_NAME} not found. use default settings.`);
|
|
440
566
|
}
|
|
441
567
|
const port = Number(specifiedPort ?? k2Config?.server?.port ?? DEFAULT_PORT);
|
|
442
|
-
const certDirPath =
|
|
443
|
-
const outputDir =
|
|
444
|
-
const inputDir =
|
|
568
|
+
const certDirPath = path10.resolve(certdir);
|
|
569
|
+
const outputDir = path10.resolve(outdir);
|
|
570
|
+
const inputDir = path10.resolve(input);
|
|
445
571
|
if (!hasCertificates(certDirPath)) {
|
|
446
|
-
console.log(
|
|
572
|
+
console.log(chalk3.yellow("\u{1F4DC} SSL certificates not found. Generating with node-forge..."));
|
|
447
573
|
try {
|
|
448
|
-
|
|
449
|
-
console.log(
|
|
574
|
+
generateCert(certDirPath);
|
|
575
|
+
console.log(chalk3.green("\u2705 SSL certificates generated successfully"));
|
|
450
576
|
} catch (error) {
|
|
451
|
-
console.log(
|
|
452
|
-
chalk4.red("\u274C Failed to generate SSL certificates. Make sure mkcert is installed.")
|
|
453
|
-
);
|
|
454
|
-
console.log(chalk4.gray(" Install mkcert: https://github.com/FiloSottile/mkcert"));
|
|
577
|
+
console.log(chalk3.red("\u274C Failed to generate SSL certificates."));
|
|
455
578
|
throw error;
|
|
456
579
|
}
|
|
457
580
|
}
|
|
458
|
-
const entries =
|
|
581
|
+
const entries = getAppEntryPoints(inputDir);
|
|
459
582
|
const entryNames = Object.keys(entries);
|
|
460
583
|
if (entryNames.length === 0) {
|
|
461
584
|
throw new Error(`No entry points found in ${input}`);
|
|
462
585
|
}
|
|
463
|
-
console.log(
|
|
464
|
-
await fs6.emptyDir(outputDir);
|
|
586
|
+
console.log(chalk3.gray(` Entry points: ${entryNames.join(", ")}`));
|
|
465
587
|
const { key, cert } = loadCertificates(certDirPath);
|
|
466
|
-
|
|
467
|
-
await buildEntriesWithVite({
|
|
588
|
+
const { port: actualPort } = await startRsbuildDevServer({
|
|
468
589
|
entries,
|
|
469
590
|
outDir: outputDir,
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
root: outputDir,
|
|
476
|
-
server: {
|
|
477
|
-
port,
|
|
478
|
-
https: { key, cert }
|
|
479
|
-
}
|
|
480
|
-
});
|
|
481
|
-
const server = await createServer(serverConfig);
|
|
482
|
-
await server.listen();
|
|
483
|
-
console.log(chalk4.green(`
|
|
591
|
+
port,
|
|
592
|
+
https: { key, cert },
|
|
593
|
+
publicDir: outputDir,
|
|
594
|
+
onFirstCompile: () => {
|
|
595
|
+
console.log(chalk3.green(`
|
|
484
596
|
\u2728 Development server ready!`));
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
persistent: true,
|
|
494
|
-
ignoreInitial: true
|
|
495
|
-
});
|
|
496
|
-
const watchExtensions = [".ts", ".tsx", ".js", ".jsx", ".css", ".scss"];
|
|
497
|
-
const shouldRebuild = (filePath) => {
|
|
498
|
-
const ext = path9.extname(filePath).toLowerCase();
|
|
499
|
-
return watchExtensions.includes(ext);
|
|
500
|
-
};
|
|
501
|
-
const rebuild = async () => {
|
|
502
|
-
console.log(chalk4.gray(` ${(/* @__PURE__ */ new Date()).toLocaleTimeString()} Rebuilding...`));
|
|
503
|
-
await buildEntriesWithVite({
|
|
504
|
-
entries,
|
|
505
|
-
outDir: outputDir,
|
|
506
|
-
mode: "development",
|
|
507
|
-
sourcemap: "inline",
|
|
508
|
-
minify: false
|
|
509
|
-
});
|
|
510
|
-
console.log(chalk4.gray(` ${(/* @__PURE__ */ new Date()).toLocaleTimeString()} Rebuild complete`));
|
|
511
|
-
};
|
|
512
|
-
watcher.on("ready", () => {
|
|
513
|
-
console.log(chalk4.green(" \u2713 File watcher ready"));
|
|
514
|
-
});
|
|
515
|
-
watcher.on("change", (filePath) => {
|
|
516
|
-
if (shouldRebuild(filePath)) {
|
|
517
|
-
console.log(chalk4.cyan(` [change] ${filePath}`));
|
|
518
|
-
rebuild();
|
|
519
|
-
}
|
|
520
|
-
});
|
|
521
|
-
watcher.on("add", (filePath) => {
|
|
522
|
-
if (shouldRebuild(filePath)) {
|
|
523
|
-
console.log(chalk4.cyan(` [add] ${filePath}`));
|
|
524
|
-
rebuild();
|
|
525
|
-
}
|
|
526
|
-
});
|
|
527
|
-
watcher.on("unlink", (filePath) => {
|
|
528
|
-
if (shouldRebuild(filePath)) {
|
|
529
|
-
console.log(chalk4.cyan(` [unlink] ${filePath}`));
|
|
530
|
-
rebuild();
|
|
597
|
+
console.log(chalk3.cyan(` Local: https://localhost:${actualPort}`));
|
|
598
|
+
console.log(chalk3.gray(` Output: ${outputDir}`));
|
|
599
|
+
console.log(chalk3.gray("\n Watching for changes...\n"));
|
|
600
|
+
},
|
|
601
|
+
onRecompile: () => {
|
|
602
|
+
console.log(
|
|
603
|
+
chalk3.hex("#e5e7eb")(`${(/* @__PURE__ */ new Date()).toLocaleTimeString()} `) + chalk3.cyan(`[rsbuild] `) + `rebuild complete`
|
|
604
|
+
);
|
|
531
605
|
}
|
|
532
606
|
});
|
|
533
|
-
watcher.on("error", (error) => {
|
|
534
|
-
console.error(chalk4.red(` Watcher error: ${error}`));
|
|
535
|
-
});
|
|
536
607
|
if (k2Config) {
|
|
537
608
|
await watchCss({ k2Config, outdir });
|
|
538
609
|
}
|
|
@@ -545,69 +616,15 @@ async function action2(options) {
|
|
|
545
616
|
|
|
546
617
|
// src/commands/genkey.ts
|
|
547
618
|
import { program as program3 } from "commander";
|
|
548
|
-
|
|
549
|
-
// src/commands/genkey-base.ts
|
|
550
|
-
async function action3(options) {
|
|
551
|
-
const { output } = options;
|
|
552
|
-
console.group("\u{1F373} Generate SSL key for localhost");
|
|
553
|
-
try {
|
|
554
|
-
const { stdout } = await generateCert(output);
|
|
555
|
-
if (stdout) {
|
|
556
|
-
console.log(stdout);
|
|
557
|
-
}
|
|
558
|
-
console.log(`\u{1F511} key generation success. Output to ./${output}`);
|
|
559
|
-
} catch (error) {
|
|
560
|
-
throw error;
|
|
561
|
-
} finally {
|
|
562
|
-
console.groupEnd();
|
|
563
|
-
}
|
|
564
|
-
}
|
|
565
|
-
|
|
566
|
-
// src/commands/genkey.ts
|
|
567
619
|
function command3() {
|
|
568
|
-
program3.command("genkey").description("Generate SSL
|
|
569
|
-
}
|
|
570
|
-
async function action4(options) {
|
|
571
|
-
await action3(options);
|
|
572
|
-
}
|
|
573
|
-
|
|
574
|
-
// src/commands/build-esbuild.ts
|
|
575
|
-
import { program as program4 } from "commander";
|
|
576
|
-
import "fs-extra";
|
|
577
|
-
import path10 from "path";
|
|
578
|
-
import chalk5 from "chalk";
|
|
579
|
-
function command4() {
|
|
580
|
-
program4.command("esbuild-build").option("-o, --outdir <outdir>", "Output directory.", path10.join(WORKSPACE_DIRECTORY, "prod")).option("-i, --input <input>", "Input directory.", path10.join("src", "apps")).option("--config <config>", "k2 config file path").description("Build the project for production with Vite. (Legacy command name, now uses Vite)").action(action5);
|
|
620
|
+
program3.command("genkey").description("Generate SSL certificate for localhost using node-forge.").option("-o, --output <output>", "Output directory.", WORKSPACE_DIRECTORY).action(action3);
|
|
581
621
|
}
|
|
582
|
-
async function
|
|
583
|
-
|
|
622
|
+
async function action3(options) {
|
|
623
|
+
const { output } = options;
|
|
624
|
+
console.group("\u{1F373} Generate SSL certificate for localhost");
|
|
584
625
|
try {
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
const entries = getEntryPointsFromDir(path10.resolve(input));
|
|
588
|
-
const entryNames = Object.keys(entries);
|
|
589
|
-
if (entryNames.length === 0) {
|
|
590
|
-
throw new Error(`No entry points found in ${input}`);
|
|
591
|
-
}
|
|
592
|
-
console.log(chalk5.gray(` Entry points: ${entryNames.join(", ")}`));
|
|
593
|
-
const k2Config = config ? await importK2Config(config) : getDefaultK2Config();
|
|
594
|
-
const fullConfig = { ...k2Config, outDir };
|
|
595
|
-
const results = await Promise.allSettled([
|
|
596
|
-
buildEntriesWithVite({
|
|
597
|
-
entries,
|
|
598
|
-
outDir,
|
|
599
|
-
mode: "production",
|
|
600
|
-
sourcemap: false,
|
|
601
|
-
minify: true
|
|
602
|
-
}),
|
|
603
|
-
buildTailwind(fullConfig)
|
|
604
|
-
]);
|
|
605
|
-
for (const result of results) {
|
|
606
|
-
if (result.status === "rejected") {
|
|
607
|
-
throw result.reason;
|
|
608
|
-
}
|
|
609
|
-
}
|
|
610
|
-
console.log("\u2728 Build success.");
|
|
626
|
+
generateCert(output);
|
|
627
|
+
console.log(`\u{1F511} Certificate generated. Output to ./${output}`);
|
|
611
628
|
} catch (error) {
|
|
612
629
|
throw error;
|
|
613
630
|
} finally {
|
|
@@ -616,14 +633,14 @@ async function action5(options) {
|
|
|
616
633
|
}
|
|
617
634
|
|
|
618
635
|
// src/commands/lint.ts
|
|
619
|
-
import { program as
|
|
636
|
+
import { program as program4 } from "commander";
|
|
620
637
|
|
|
621
638
|
// src/lib/lint.ts
|
|
622
639
|
import { ESLint } from "eslint";
|
|
623
640
|
import globals from "globals";
|
|
624
641
|
import pluginJs from "@eslint/js";
|
|
625
642
|
import "typescript-eslint";
|
|
626
|
-
import
|
|
643
|
+
import pluginReact2 from "eslint-plugin-react";
|
|
627
644
|
async function lint() {
|
|
628
645
|
const eslint = new ESLint({
|
|
629
646
|
baseConfig: [
|
|
@@ -631,7 +648,7 @@ async function lint() {
|
|
|
631
648
|
{ languageOptions: { globals: globals.browser } },
|
|
632
649
|
pluginJs.configs.recommended,
|
|
633
650
|
// ...tseslint.configs.recommended,
|
|
634
|
-
...
|
|
651
|
+
...pluginReact2.configs.flat?.recommended ? [pluginReact2.configs.flat.recommended] : []
|
|
635
652
|
]
|
|
636
653
|
});
|
|
637
654
|
const results = await eslint.lintFiles(["src/**/*.{ts,tsx?}"]);
|
|
@@ -648,10 +665,10 @@ async function lint() {
|
|
|
648
665
|
}
|
|
649
666
|
|
|
650
667
|
// src/commands/lint.ts
|
|
651
|
-
function
|
|
652
|
-
|
|
668
|
+
function command4() {
|
|
669
|
+
program4.command("lint").description("Lint source files").option("-c, --config <config>", "Config file path").action(action4);
|
|
653
670
|
}
|
|
654
|
-
async function
|
|
671
|
+
async function action4(options) {
|
|
655
672
|
try {
|
|
656
673
|
lint();
|
|
657
674
|
} catch (error) {
|
|
@@ -661,11 +678,10 @@ async function action6(options) {
|
|
|
661
678
|
}
|
|
662
679
|
|
|
663
680
|
// src/index.ts
|
|
664
|
-
|
|
681
|
+
program5.name("k2").version("4.0.0").description("k2 - \u{1F373} kintone kitchen \u{1F373}");
|
|
665
682
|
command();
|
|
666
|
-
command4();
|
|
667
683
|
command2();
|
|
668
684
|
command3();
|
|
669
|
-
|
|
670
|
-
|
|
685
|
+
command4();
|
|
686
|
+
program5.parse(process.argv);
|
|
671
687
|
//# sourceMappingURL=index.js.map
|