@jay-framework/jay-cli 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/dist/index.js +107 -0
  2. package/package.json +38 -0
  3. package/readme.md +26 -0
package/dist/index.js ADDED
@@ -0,0 +1,107 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ const rollup = require("rollup");
4
+ const commander = require("commander");
5
+ const compiler = require("@jay-framework/compiler");
6
+ const rollupPlugin = require("@jay-framework/rollup-plugin");
7
+ const chalk = require("chalk");
8
+ const fastGlob = require("fast-glob");
9
+ const compilerShared = require("@jay-framework/compiler-shared");
10
+ const fs = require("fs");
11
+ const path = require("path");
12
+ const compilerJayHtml = require("@jay-framework/compiler-jay-html");
13
+ const path$1 = require("node:path");
14
+ async function findAllJayFiles(dir) {
15
+ return await fastGlob.glob(`${dir}/**/*${compilerShared.JAY_EXTENSION}`);
16
+ }
17
+ function checkFileExists(filepath) {
18
+ return new Promise((resolve, reject) => {
19
+ fs.access(filepath, fs.constants.F_OK, (error) => {
20
+ resolve(!error);
21
+ });
22
+ });
23
+ }
24
+ async function generateFiles(dir, codeGenerationFunction, afterGenerationFunction, outputExtension, destinationDir, compilationTarget) {
25
+ console.log(chalk.whiteBright("Jay generating files for ", dir));
26
+ let jayFiles = await findAllJayFiles(dir);
27
+ console.log(dir, jayFiles);
28
+ let generationFailed = false;
29
+ for (const jayFile of jayFiles) {
30
+ const content = await fs.promises.readFile(jayFile, "utf-8");
31
+ const parsedFile = compilerShared.checkValidationErrors(
32
+ await compilerJayHtml.parseJayFile(
33
+ content,
34
+ path.basename(jayFile.replace(".jay-html", "")),
35
+ path.dirname(jayFile),
36
+ {},
37
+ compilerJayHtml.JAY_IMPORT_RESOLVER
38
+ )
39
+ );
40
+ const generateTarget = compilationTarget === "react" ? compilerShared.GenerateTarget.react : compilerShared.GenerateTarget.jay;
41
+ const generatedFile = codeGenerationFunction(
42
+ parsedFile,
43
+ compilerShared.RuntimeMode.MainTrusted,
44
+ generateTarget
45
+ );
46
+ const generateFileName = jayFile + outputExtension;
47
+ if (generatedFile.validations.length > 0) {
48
+ console.log(
49
+ `${chalk.red("failed to generate")} ${chalk.yellow(jayFile)} → ${chalk.yellow(
50
+ generateFileName
51
+ )}`
52
+ );
53
+ generatedFile.validations.forEach((_) => console.log(chalk.red(_)));
54
+ generationFailed = true;
55
+ } else {
56
+ console.log(
57
+ `${chalk.green("generated")} ${chalk.yellow(jayFile)} → ${chalk.yellow(
58
+ generateFileName
59
+ )}`
60
+ );
61
+ let destinationGeneratedFileName;
62
+ if (destinationDir) {
63
+ let absGeneratedFileName = path.resolve(generateFileName);
64
+ let absSourceDir = path.resolve(dir);
65
+ let absDestDir = path.resolve(destinationDir);
66
+ destinationGeneratedFileName = absGeneratedFileName.replace(
67
+ absSourceDir,
68
+ absDestDir
69
+ );
70
+ } else
71
+ destinationGeneratedFileName = generateFileName;
72
+ let destinationDirName = path.dirname(destinationGeneratedFileName);
73
+ if (!await checkFileExists(destinationDirName)) {
74
+ await fs.promises.mkdir(destinationDirName, { recursive: true });
75
+ }
76
+ await fs.promises.writeFile(destinationGeneratedFileName, generatedFile.val);
77
+ }
78
+ afterGenerationFunction(
79
+ content,
80
+ path.basename(jayFile.replace(".jay-html", "")),
81
+ path.dirname(jayFile)
82
+ );
83
+ if (generationFailed) {
84
+ throw new Error("Jay file generation failed, please fix the issues above.");
85
+ }
86
+ }
87
+ }
88
+ function getJayHtmlOrContractFileInputs(source) {
89
+ return Object.fromEntries(
90
+ fastGlob.globSync(`${source}/**/*{${compilerShared.JAY_EXTENSION},${compilerShared.JAY_CONTRACT_EXTENSION}}`).map((file) => {
91
+ const moduleName = file.includes(compilerShared.JAY_EXTENSION) ? file.slice(0, file.length - compilerShared.JAY_EXTENSION.length) : file.slice(0, file.length - compilerShared.JAY_CONTRACT_EXTENSION.length);
92
+ return [path$1.relative(source, moduleName), file];
93
+ })
94
+ );
95
+ }
96
+ const program = new commander.Command();
97
+ const noop = () => void 0;
98
+ program.command("definitions").argument("<source>", "source folder to scan for .jay-html files").description("generate definition files (.d.ts) for jay files").action(async (source) => {
99
+ await rollup.rollup({
100
+ input: getJayHtmlOrContractFileInputs(source),
101
+ plugins: [rollupPlugin.jayDefinitions()]
102
+ });
103
+ });
104
+ program.command("runtime").argument("<source>", "source folder to scan for .jay-html files").argument("[destination]", "destination folder for generated files").argument("[compilationTarget]", "jay | react. target runtime to compile for. Defaults to jay").description("generate code files (.ts) for jay files").action(async (source, dest, compilationTarget) => {
105
+ await generateFiles(source, compiler.generateElementFile, noop, ".ts", dest, compilationTarget);
106
+ });
107
+ program.parse(process.argv);
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@jay-framework/jay-cli",
3
+ "version": "0.5.0",
4
+ "license": "Apache-2.0",
5
+ "main": "dist/index.js",
6
+ "bin": "dist/index.js",
7
+ "files": [
8
+ "dist",
9
+ "readme.md"
10
+ ],
11
+ "scripts": {
12
+ "build": "npm run build:cli-link && npm run build:js",
13
+ "build:watch": "npm run build:cli-link && npm run build:js -- --watch",
14
+ "build:js": "vite build",
15
+ "build:check-types": "tsc",
16
+ "build:cli-link": "cd ../../../node_modules/.bin && ln -sf ../../../packages/cli/dist/index.js jay-cli",
17
+ "clean": "rimraf dist",
18
+ "confirm": "npm run clean && npm run build && npm run build:check-types && npm run test",
19
+ "test": ":",
20
+ "test:watch": ":"
21
+ },
22
+ "dependencies": {
23
+ "@jay-framework/compiler": "workspace:^",
24
+ "@jay-framework/rollup-plugin": "workspace:^",
25
+ "chalk": "^4.1.2",
26
+ "commander": "^14.0.0",
27
+ "rollup": "^4.9.5"
28
+ },
29
+ "devDependencies": {
30
+ "@jay-framework/dev-environment": "workspace:^",
31
+ "@types/node": "^20.11.5",
32
+ "@types/shelljs": "^0.8.15",
33
+ "rimraf": "^5.0.5",
34
+ "shelljs": "^0.8.5",
35
+ "typescript": "^5.3.3",
36
+ "vite": "^5.0.11"
37
+ }
38
+ }
package/readme.md ADDED
@@ -0,0 +1,26 @@
1
+ # jay cli
2
+
3
+ The jay cli is a utility for build and dev time that generate files from jay-html files.
4
+
5
+ ## definitions
6
+
7
+ The definitions command generates the `.d.ts` files for `.jay-html` files, which are then
8
+ used for development. The `.d.ts` files are placed along side the `.jay-html` files.
9
+
10
+ ```shell
11
+ jay-cli definitions source
12
+ ```
13
+
14
+ - `source` - the source folder to scan for `.jay-html` files
15
+
16
+ ## runtime
17
+
18
+ The runtime command generates the runtime files for jay-html files.
19
+ Normally, those files are created by the vite-plugin and not created explicitly.
20
+
21
+ ```shell
22
+ jay-cli runtime source destination
23
+ ```
24
+
25
+ - `source` - the source folder to scan for `.jay-html` files
26
+ - `destination` - the folder to create the files in