@digigov/cli-build 1.1.0 → 2.0.0-07ee8440

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/CHANGELOG.json CHANGED
@@ -1,6 +1,18 @@
1
1
  {
2
2
  "name": "@digigov/cli-build",
3
3
  "entries": [
4
+ {
5
+ "version": "1.1.1",
6
+ "tag": "@digigov/cli-build_v1.1.1",
7
+ "date": "Mon, 29 Jan 2024 17:45:11 GMT",
8
+ "comments": {
9
+ "patch": [
10
+ {
11
+ "comment": "Add react-experimental folder"
12
+ }
13
+ ]
14
+ }
15
+ },
4
16
  {
5
17
  "version": "1.1.0",
6
18
  "tag": "@digigov/cli-build_v1.1.0",
package/CHANGELOG.md CHANGED
@@ -1,6 +1,13 @@
1
1
  # Change Log - @digigov/cli-build
2
2
 
3
- This log was last generated on Mon, 29 Jan 2024 10:46:50 GMT and should not be manually modified.
3
+ This log was last generated on Mon, 29 Jan 2024 17:45:11 GMT and should not be manually modified.
4
+
5
+ ## 1.1.1
6
+ Mon, 29 Jan 2024 17:45:11 GMT
7
+
8
+ ### Patches
9
+
10
+ - Add react-experimental folder
4
11
 
5
12
  ## 1.1.0
6
13
  Mon, 29 Jan 2024 10:46:50 GMT
@@ -4,7 +4,7 @@ const lib = require("@digigov/cli/lib");
4
4
 
5
5
  function makeBabelConfig(dir, opts = { docs: false, proptypes: false }) {
6
6
  const project = lib.resolveProject(dir);
7
- const aliases = !project.externalLockFile ? lib.aliases(null, true) : {};
7
+ const aliases = !project.externalLockFile ? lib.aliases(true) : {};
8
8
 
9
9
  const BABEL_ENV = process.env.BABEL_ENV || "esm";
10
10
  const BABEL_PUBLISH = process.env.BABEL_PUBLISH || false;
@@ -0,0 +1 @@
1
+ module.exports = require("./babel.common.cjs").config;
package/build.js ADDED
@@ -0,0 +1,85 @@
1
+ import { DigigovCommand, logger } from "@digigov/cli/lib";
2
+
3
+ import assert from "assert";
4
+ import path from "path";
5
+ import fs from "fs-extra";
6
+ import baseEsbuild from "esbuild";
7
+
8
+ /**
9
+ * Generate TypeScript declaration files
10
+ *
11
+ * @param {object} project - The project object
12
+ * @param {string} project.root - The project root directory
13
+ * @param {string} project.src - The project source directory
14
+ * @param {string} project.distDir - The project build directory
15
+ * @param {string} tsconfig - The tsconfig path
16
+ * @param {DigigovCommand} ctx - The command context
17
+ */
18
+ export async function generateTypeDeclarationFiles(project, tsconfig, ctx) {
19
+ logger.debug("Building types...");
20
+
21
+ const distDir = path.resolve(project.root, project.distDir);
22
+ const projectBasename = path.basename(project.root);
23
+
24
+ await ctx.exec("tsc", [
25
+ "--emitDeclarationOnly",
26
+ "--outDir",
27
+ "dist",
28
+ "--project",
29
+ tsconfig,
30
+ ]);
31
+
32
+ const projectBasePath = path.join(distDir, projectBasename);
33
+ logger.debug("Project base path", projectBasePath);
34
+ if (await fs.exists(projectBasePath)) {
35
+ const typesIncluded = await fs.readdir(path.join(distDir));
36
+ const srcPath = path.join(distDir, projectBasename, project.src);
37
+ const paths = await fs.readdir(srcPath);
38
+
39
+ await Promise.all([
40
+ // Move src files to dist
41
+ ...paths.map((p) => {
42
+ logger.debug("Moving types file", p);
43
+ fs.move(path.join(srcPath, p), path.join(distDir, p));
44
+ }),
45
+ // Remove dirs
46
+ ...typesIncluded.map((typesDir) => {
47
+ logger.debug("Removing types directory", typesDir);
48
+ fs.rm(path.join(distDir, typesDir), { recursive: true });
49
+ }),
50
+ ]).catch((err) => {
51
+ logger.error("Error while building types", err);
52
+ });
53
+ }
54
+ logger.debug("Types built.");
55
+ }
56
+
57
+ /**
58
+ * Run esbuild for the given options
59
+ *
60
+ * @param {object} options - The build options
61
+ * @param {string[]} options.files - The files to build
62
+ * @param {string | undefined} options.tsconfig - The tsconfig path
63
+ * @param {"esm" | "cjs"} options.format - The module format
64
+ * @param {string} options.outdir - The output directory
65
+ */
66
+ export function buildFormat({ files: entryPoints, tsconfig, format, outdir }) {
67
+ assert(format === "esm" || format === "cjs", "Invalid format");
68
+
69
+ logger.log(`Running: esbuild for ${format.toUpperCase()} format`);
70
+ return baseEsbuild.build({
71
+ ...BASE_OPTIONS,
72
+ entryPoints,
73
+ tsconfig,
74
+ format,
75
+ outdir,
76
+ });
77
+ }
78
+
79
+ /** @type {baseEsbuild.BuildOptions} */
80
+ export const BASE_OPTIONS = {
81
+ logLevel: "error",
82
+ platform: "node",
83
+ sourcemap: true,
84
+ target: ["esnext"],
85
+ };
package/common.js ADDED
@@ -0,0 +1,20 @@
1
+ import fs from "fs-extra";
2
+ import path from "path";
3
+
4
+ const POSSIBLE_TS_CONFIGS = ["tsconfig.production.json", "tsconfig.json"];
5
+
6
+ /**
7
+ * Get the tsconfig path for the given project
8
+ *
9
+ * @param {string} projectRoot - The project root
10
+ * @returns {string | undefined} - The tsconfig path or undefined if not found
11
+ */
12
+ export function getProjectTsconfig(projectRoot) {
13
+ for (const tsconfig of POSSIBLE_TS_CONFIGS) {
14
+ const tsconfigPath = path.join(projectRoot, tsconfig);
15
+ if (fs.existsSync(tsconfigPath)) {
16
+ return tsconfigPath;
17
+ }
18
+ }
19
+ return;
20
+ }
package/copy-files.js CHANGED
@@ -1,23 +1,40 @@
1
- /* eslint-disable no-console */
2
- const path = require("path");
3
- const fs = require("fs");
4
- const fse = require("fs-extra");
5
- const glob = require("glob");
6
- const { resolveProject } = require("@digigov/cli/lib");
1
+ import { logger, resolveProject } from "@digigov/cli/lib";
2
+ import fs from "fs-extra";
3
+ import path from "path";
4
+ import glob from "globby";
7
5
 
8
- const project = resolveProject();
9
6
  const packagePath = process.cwd();
7
+ const project = resolveProject();
10
8
  const buildPath = path.join(project.root, project.distDir);
11
9
 
12
- async function includeFileInBuild(file) {
10
+ /**
11
+ * Copy a file from the package to the build directory
12
+ *
13
+ * @param {string} file - The file to include in the build
14
+ */
15
+ function includeFileInBuild(file) {
13
16
  const sourcePath = path.resolve(packagePath, file);
14
17
  const targetPath = path.resolve(buildPath, path.basename(file));
15
- await fse.copy(sourcePath, targetPath);
16
- console.log(`Copied ${sourcePath} to ${targetPath}`);
18
+ fs.copySync(sourcePath, targetPath);
19
+ logger.debug(`Copied ${sourcePath} to build directory`);
20
+ }
21
+
22
+ function copyRegistryFilesToSrc() {
23
+ const registryPath = path.resolve(buildPath, "registry/index.js");
24
+ const lazyPath = path.resolve(buildPath, "lazy/index.js");
25
+ if (!fs.existsSync(registryPath) || !fs.existsSync(lazyPath)) return;
26
+
27
+ const srcPath = path.resolve(buildPath, "src");
28
+ logger.debug(`Copying registry and lazy files to ${srcPath}`);
29
+ fs.copySync(registryPath, path.resolve(srcPath, "registry.js"));
30
+ fs.copySync(lazyPath, path.resolve(srcPath, "lazy.js"));
17
31
  }
18
32
 
19
- async function createRootPackageFile() {
20
- const packageData = await fse.readFile(
33
+ /**
34
+ * Create a package.json file in the build directory
35
+ */
36
+ function createRootPackageFile() {
37
+ const packageData = fs.readFileSync(
21
38
  path.resolve(packagePath, "./package.json"),
22
39
  "utf8",
23
40
  );
@@ -32,18 +49,20 @@ async function createRootPackageFile() {
32
49
  };
33
50
  const targetPath = path.resolve(buildPath, "./package.json");
34
51
 
35
- await fse.writeFile(
36
- targetPath,
37
- JSON.stringify(newPackageData, null, 2),
38
- "utf8",
39
- );
40
- console.log(`Created package.json in ${targetPath}`);
52
+ fs.writeFileSync(targetPath, JSON.stringify(newPackageData, null, 2), "utf8");
53
+ logger.debug(`Created package.json in build directory`);
41
54
 
42
55
  return newPackageData;
43
56
  }
44
57
 
58
+ /**
59
+ * Create nested package.json files in the build directory
60
+ *
61
+ */
45
62
  function createNestedPackageFiles() {
46
- const indexPaths = glob.sync(path.join(buildPath, "!(cjs)/**/index.js"));
63
+ const indexPaths = glob.sync(path.join(buildPath, "**/index.js"), {
64
+ ignore: [path.join(buildPath, "cjs/**")],
65
+ });
47
66
 
48
67
  indexPaths.forEach((indexPath) => {
49
68
  if (indexPath === path.join(buildPath, "index.js")) return;
@@ -63,63 +82,78 @@ function createNestedPackageFiles() {
63
82
  });
64
83
  }
65
84
 
66
- async function prepend(file, string) {
67
- const data = await fse.readFile(file, "utf8");
68
- await fse.writeFile(file, string + data, "utf8");
85
+ /**
86
+ * Prepend a string to a file
87
+ *
88
+ * @param {string} file - The file to prepend to
89
+ * @param {string} string - The string to prepend
90
+ */
91
+ function prepend(file, string) {
92
+ const data = fs.readFileSync(file, "utf8");
93
+ fs.writeFileSync(file, string + data, "utf8");
94
+ logger.debug(`Prepended license to ${file}`);
69
95
  }
70
96
 
71
- async function addLicense(packageData) {
72
- const license = `/** @license Digigov v${packageData.version}
97
+ /**
98
+ * Add license to the top of the files
99
+ *
100
+ * @param {object} packageData - The package data
101
+ */
102
+ function addLicense(packageData) {
103
+ const license = `/** @license Digigov v${packageData["version"]}
73
104
  *
74
105
  * This source code is licensed under the BSD-2-Clause license found in the
75
106
  * LICENSE file in the root directory of this source tree.
76
107
  */
77
108
  `;
78
- await Promise.all(
79
- ["./index.js", "./index.mjs"].map(async (file) => {
80
- try {
81
- await prepend(path.resolve(buildPath, file), license);
82
- } catch (err) {
83
- if (err.code === "ENOENT") {
84
- console.log(`Skipped license for ${file}`);
85
- } else {
86
- throw err;
87
- }
109
+ ["./index.js", "./index.mjs"].map(async (file) => {
110
+ try {
111
+ prepend(path.resolve(buildPath, file), license);
112
+ } catch (err) {
113
+ if (
114
+ typeof err === "object" &&
115
+ err &&
116
+ "code" in err &&
117
+ err.code === "ENOENT"
118
+ ) {
119
+ logger.debug(`Skipped license for ${file}`);
120
+ } else {
121
+ throw err;
88
122
  }
89
- }),
90
- );
123
+ }
124
+ });
91
125
  }
92
126
 
127
+ /**
128
+ * Create separate index modules for each directory
129
+ */
93
130
  function createSeparateIndexModules() {
94
- const files = glob.sync(path.join(buildPath, "**/!(index).js"));
131
+ const files = glob.sync(path.join(buildPath, "**/*.js"), {
132
+ ignore: [path.join(buildPath, "**/index.js")],
133
+ });
95
134
 
96
135
  files.forEach((file) => {
97
- fs.mkdirSync(file.replace(".js", ""));
98
- fs.renameSync(file, file.replace(".js", "/index.js"));
136
+ fs.mkdirSync(file.replace(/\.js$/, ""));
137
+ fs.renameSync(file, file.replace(/\.js$/, "/index.js"));
99
138
  });
100
139
  }
101
140
 
102
- async function run() {
103
- try {
104
- const packageData = await createRootPackageFile();
105
- createSeparateIndexModules();
106
- createNestedPackageFiles();
107
-
108
- await Promise.all(
109
- [
110
- // use enhanced readme from workspace root for `@digigov/ui`
111
- // packageData.name === '@digigov/ui' ? '../../README.md' : './README.md',
112
- "./src",
113
- "./README.md",
114
- "./CHANGELOG.md",
115
- "../../LICENSE",
116
- ].map((file) => includeFileInBuild(file)),
117
- );
141
+ /**
142
+ * Run the copy files script
143
+ */
144
+ export default function run() {
145
+ const packageData = createRootPackageFile();
146
+ createSeparateIndexModules();
147
+ createNestedPackageFiles();
148
+ copyRegistryFilesToSrc();
118
149
 
119
- await addLicense(packageData);
120
- } catch (err) {
121
- console.error(err);
122
- }
150
+ [
151
+ // use enhanced readme from workspace root for `@digigov/ui`
152
+ // packageData.name === '@digigov/ui' ? '../../README.md' : './README.md',
153
+ "./src",
154
+ "./README.md",
155
+ "./CHANGELOG.md",
156
+ "../../LICENSE",
157
+ ].map((file) => includeFileInBuild(file)),
158
+ addLicense(packageData);
123
159
  }
124
-
125
- run();
@@ -0,0 +1,268 @@
1
+ import { logger } from "@digigov/cli/lib";
2
+ import path from "path";
3
+ import fs from "fs-extra";
4
+ import { SyntaxKind, Project as TsMorphProject } from "ts-morph";
5
+ import assert from "assert";
6
+
7
+ import { getProjectTsconfig } from "./common.js";
8
+
9
+ /**
10
+ * Generate registry files for the given project
11
+ *
12
+ * @param {object} project - The project object
13
+ * @param {string} project.root - The project root directory
14
+ * @param {string} project.distDir - The project build directory
15
+ * @param {string} project.name - The project name as in package.json
16
+ * @param {string[]} filePaths - The paths of the files to include in the registry
17
+ * @param {boolean} shouldGenerateStoriesRegistry - Whether to export stories in the registry
18
+ * @returns {Promise<[string, string]>} - The paths of the generated registry files
19
+ */
20
+ export async function generateRegistryFiles(
21
+ project,
22
+ filePaths,
23
+ shouldGenerateStoriesRegistry = false,
24
+ ) {
25
+ const registryPath = ensureRegistryPath(project, "registry.js");
26
+ const lazyRegistryPath = ensureRegistryPath(project, "lazy.js");
27
+
28
+ const registry = generateRegistryFileContent(
29
+ project,
30
+ filePaths,
31
+ shouldGenerateStoriesRegistry,
32
+ );
33
+
34
+ const componentPathsOnly = filePaths.filter(
35
+ (path) => !path.includes("stories"),
36
+ );
37
+ const lazyRegistry = generateLazyFileContent(project, componentPathsOnly);
38
+
39
+ await Promise.all([
40
+ fs.writeFile(registryPath, registry),
41
+ fs.writeFile(lazyRegistryPath, lazyRegistry),
42
+ ]);
43
+
44
+ return [registryPath, lazyRegistryPath];
45
+ }
46
+
47
+ /**
48
+ * Ensure that the registry file does not already exist at the given path
49
+ *
50
+ * @param {object} project - The project object
51
+ * @param {string} project.root - The project root directory
52
+ * @param {string} project.distDir - The project build directory
53
+ * @param {string} fileName - The name of the registry file
54
+ */
55
+ function ensureRegistryPath(project, fileName) {
56
+ const registryPath = path.join(project.root, project.distDir, fileName);
57
+ if (fs.existsSync(registryPath))
58
+ throw new Error(`A "${fileName}" file already exists at ${registryPath}.`);
59
+ return registryPath;
60
+ }
61
+
62
+ /**
63
+ * Generate registry file content for the given files
64
+ *
65
+ * @param {object} project - The project object
66
+ * @param {string} project.root - The project root directory
67
+ * @param {string} project.name - The project name as in package.json
68
+ * @param {string[]} absoluteFilePaths - The absolute paths of the files to include in the registry
69
+ * @param {boolean} includeStories - Whether to include stories in the registry
70
+ * @returns {string} - The registry file content or null if no components are found
71
+ */
72
+ export function generateRegistryFileContent(
73
+ project,
74
+ absoluteFilePaths,
75
+ includeStories = false,
76
+ ) {
77
+ const relativePaths = absoluteFilePaths.map((path) => {
78
+ assert(
79
+ path.startsWith(project.root),
80
+ "Expected path to be in project root",
81
+ );
82
+ return toNodeResolvablePath(
83
+ path.replace(`${project.root}/src/`, `${project.name}/`),
84
+ );
85
+ });
86
+ let registryPaths = relativePaths.map((path) => ({
87
+ path,
88
+ uid: createUid(path),
89
+ }));
90
+
91
+ if (registryPaths.length === 0)
92
+ throw new Error(
93
+ "Could not generate registry. No exportable modules found.",
94
+ );
95
+
96
+ const importStatements = registryPaths.map(
97
+ (file) => `import * as ${file.uid} from "${file.path}";`,
98
+ );
99
+ const exportStatements = registryPaths.map(
100
+ (file) => ` '${file.path}': lazyImport(${file.uid})`,
101
+ );
102
+
103
+ const [components, stories] = splitStoriesExports(exportStatements);
104
+ logger.debug(`Including ${components.length} component modules in registry`);
105
+
106
+ if (includeStories) {
107
+ logger.debug(`Including ${stories.length} stories in registry`);
108
+ }
109
+
110
+ let out = `
111
+ ${importStatements.join("\n")}
112
+ function lazyImport(pkgImport) {
113
+ return new Proxy(
114
+ {},
115
+ {
116
+ get: (_target, name) => {
117
+ if (name === '__esModule' || name === 'default') {
118
+ return pkgImport.default;
119
+ } else if(
120
+ name === '*'
121
+ ) {
122
+ return pkgImport;
123
+ } else {
124
+ return pkgImport[name];
125
+ }
126
+ },
127
+ }
128
+ )
129
+ }
130
+ export default {
131
+ ${components.join(",\n")}
132
+ };
133
+ `;
134
+
135
+ if (includeStories) {
136
+ out += `
137
+
138
+ export const stories = {
139
+ ${stories.join(",\n")}
140
+ };
141
+ `;
142
+ }
143
+ return out;
144
+ }
145
+
146
+ /**
147
+ * Extract a node-resolvable path
148
+ *
149
+ * @param {string} inputPath - The file path
150
+ * @returns {string} - The node-resolvable path
151
+ */
152
+ export function toNodeResolvablePath(inputPath) {
153
+ const dir = path.dirname(inputPath);
154
+ const base = path.basename(inputPath, path.extname(inputPath));
155
+
156
+ return base === "index" ? dir : path.join(dir, base);
157
+ }
158
+
159
+ /**
160
+ * Create a UID from a path
161
+ *
162
+ * @param {string} inputPath - The path
163
+ * @returns {string} - The UID
164
+ */
165
+ export function createUid(inputPath) {
166
+ return inputPath.replace(/[\/@\-.]/g, "_");
167
+ }
168
+
169
+ /**
170
+ * Split the given files into components and stories
171
+ *
172
+ * @param {string[]} exportStatements - The export statements
173
+ * @returns {[string[], string[]]} - The split components and stories exports
174
+ */
175
+ export function splitStoriesExports(exportStatements) {
176
+ const stories = [];
177
+ const components = [];
178
+ for (const exportStatement of exportStatements) {
179
+ if (exportStatement.includes("_stories")) {
180
+ stories.push(exportStatement);
181
+ } else {
182
+ components.push(exportStatement);
183
+ }
184
+ }
185
+ return [components, stories];
186
+ }
187
+
188
+ /**
189
+ * Generate lazy component registry file content for the given project
190
+ *
191
+ * @param {object} project - The project object
192
+ * @param {string} project.root - The project root directory
193
+ * @param {string} project.name - The project name as in package.json
194
+ * @param {string[]} filePaths - The files whose exports will be included in the lazy registry
195
+ * @returns {string} - The lazy component registry file content or null if no components are found
196
+ */
197
+ export function generateLazyFileContent(project, filePaths) {
198
+ const tsMorphProject = new TsMorphProject({
199
+ tsConfigFilePath: getProjectTsconfig(project.root),
200
+ });
201
+
202
+ /** @type {Record<string, string>} */
203
+ let allComponents = {};
204
+
205
+ for (const filePath of filePaths) {
206
+ const sourceFile = tsMorphProject.addSourceFileAtPath(filePath);
207
+ const exports = sourceFile
208
+ .getExportSymbols()
209
+ .filter(isJsExport)
210
+ .map((symbol) => symbol.getName());
211
+
212
+ for (const exportedComponent of exports) {
213
+ if (
214
+ exportedComponent !== "default" &&
215
+ exportedComponent.match(/^[A-Z]/)
216
+ ) {
217
+ if (
218
+ !allComponents[exportedComponent] ||
219
+ allComponents[exportedComponent].length < filePath.length // Make import path more specific
220
+ ) {
221
+ allComponents[exportedComponent] = toNodeResolvablePath(
222
+ filePath.replace(`${project.root}/src/`, `${project.name}/`),
223
+ );
224
+ }
225
+ }
226
+ }
227
+ }
228
+
229
+ const componentCount = Object.keys(allComponents).length;
230
+
231
+ if (componentCount === 0)
232
+ throw new Error(
233
+ "Could not generate lazy registry. No exportable components found.",
234
+ );
235
+
236
+ logger.debug(`Including ${componentCount} components in lazy registry`);
237
+
238
+ const content = Object.entries(allComponents)
239
+ .map(
240
+ ([component, filePath]) =>
241
+ ` '${component}': lazy(() => import('${filePath}').then((module) => ({ default: module['${component}'] })))`,
242
+ )
243
+ .join(",\n");
244
+
245
+ return `import { lazy } from 'react';
246
+ export default {
247
+ ${content}
248
+ };
249
+ `;
250
+ }
251
+
252
+ /**
253
+ * Check if a symbol is a JS export
254
+ *
255
+ * @param {import("ts-morph").Symbol} symbol - The symbol to check
256
+ */
257
+ function isJsExport(symbol) {
258
+ const declarations = symbol.getDeclarations();
259
+ return declarations.some((declaration) => {
260
+ const kind = declaration.getKind();
261
+ return (
262
+ kind === SyntaxKind.FunctionDeclaration ||
263
+ kind === SyntaxKind.ClassDeclaration ||
264
+ kind === SyntaxKind.VariableDeclaration ||
265
+ kind === SyntaxKind.ExportSpecifier
266
+ );
267
+ });
268
+ }