@digigov/cli-build 2.0.0-750aec28 → 2.0.0-76ec20a0

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/build.js CHANGED
@@ -62,11 +62,19 @@ export async function generateTypeDeclarationFiles(project, tsconfig, ctx) {
62
62
  * @param {string | undefined} options.tsconfig - The tsconfig path
63
63
  * @param {"esm" | "cjs"} options.format - The module format
64
64
  * @param {string} options.outdir - The output directory
65
+ * @param {boolean | undefined} [options.noLogs] - Whether to log debug information
65
66
  */
66
- export function buildFormat({ files: entryPoints, tsconfig, format, outdir }) {
67
+ export function buildFormat({
68
+ files: entryPoints,
69
+ tsconfig,
70
+ format,
71
+ outdir,
72
+ noLogs,
73
+ }) {
67
74
  assert(format === "esm" || format === "cjs", "Invalid format");
68
75
 
69
- logger.log(`Running: esbuild for ${format.toUpperCase()} format`);
76
+ if (!noLogs)
77
+ logger.log(`Running: esbuild for ${format.toUpperCase()} format`);
70
78
  return baseEsbuild.build({
71
79
  ...BASE_OPTIONS,
72
80
  entryPoints,
@@ -6,74 +6,27 @@ import assert from "assert";
6
6
 
7
7
  import { getProjectTsconfig } from "./common.js";
8
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
9
+ /** @typedef {Object} Project - Represents the project to be built
10
+ * @property {string} root - The project root directory
11
+ * @property {string} name - The project name as in package.json
12
+ * @property {string} distDir - The project build directory
19
13
  */
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
14
 
47
15
  /**
48
- * Ensure that the registry file does not already exist at the given path
16
+ * Generate registry file for the given project
49
17
  *
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
18
+ * @param {Project} project - The project object
19
+ * @param {string} [registryFilename="registry.js"] - The name of the registry file
68
20
  * @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
21
+ * @returns {Promise<string>} - The path to the generated registry file
71
22
  */
72
- export function generateRegistryFileContent(
23
+ export async function generateRegistry(
73
24
  project,
74
25
  absoluteFilePaths,
75
- includeStories = false,
26
+ registryFilename = "registry.js",
76
27
  ) {
28
+ const registryPath = ensureRegistryPath(project, registryFilename);
29
+
77
30
  const relativePaths = absoluteFilePaths.map((path) => {
78
31
  assert(
79
32
  path.startsWith(project.root),
@@ -96,18 +49,15 @@ export function generateRegistryFileContent(
96
49
  const importStatements = registryPaths.map(
97
50
  (file) => `import * as ${file.uid} from "${file.path}";`,
98
51
  );
99
- const exportStatements = registryPaths.map(
52
+ const componentsToExport = registryPaths.map(
100
53
  (file) => ` '${file.path}': lazyImport(${file.uid})`,
101
54
  );
102
55
 
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
- }
56
+ logger.debug(
57
+ `Including ${componentsToExport.length} items in ${registryPath}`,
58
+ );
109
59
 
110
- let out = `
60
+ let registryFileContent = `
111
61
  ${importStatements.join("\n")}
112
62
  function lazyImport(pkgImport) {
113
63
  return new Proxy(
@@ -128,73 +78,29 @@ function lazyImport(pkgImport) {
128
78
  )
129
79
  }
130
80
  export default {
131
- ${components.join(",\n")}
132
- };
133
- `;
134
-
135
- if (includeStories) {
136
- out += `
137
-
138
- export const stories = {
139
- ${stories.join(",\n")}
81
+ ${componentsToExport.join(",\n")}
140
82
  };
141
83
  `;
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
- }
84
+ await fs.writeFile(registryPath, registryFileContent);
168
85
 
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];
86
+ return registryPath;
186
87
  }
187
88
 
188
89
  /**
189
- * Generate lazy component registry file content for the given project
90
+ * Generate a lazy registry file for the given project
190
91
  *
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
92
+ * @param {Project} project - The project object
194
93
  * @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
94
+ * @param {string} [lazyFilename="lazy.js"] - The name of the registry file
95
+ * @returns {Promise<string>} - The path to the generated lazy registry file
196
96
  */
197
- export function generateLazyFileContent(project, filePaths) {
97
+ export async function generateLazyRegistry(
98
+ project,
99
+ filePaths,
100
+ lazyFilename = "lazy.js",
101
+ ) {
102
+ const lazyPath = ensureRegistryPath(project, lazyFilename);
103
+
198
104
  const tsMorphProject = new TsMorphProject({
199
105
  tsConfigFilePath: getProjectTsconfig(project.root),
200
106
  });
@@ -233,20 +139,60 @@ export function generateLazyFileContent(project, filePaths) {
233
139
  "Could not generate lazy registry. No exportable components found.",
234
140
  );
235
141
 
236
- logger.debug(`Including ${componentCount} components in lazy registry`);
142
+ logger.debug(`Including ${componentCount} components in ${lazyPath}`);
237
143
 
238
- const content = Object.entries(allComponents)
144
+ const componentsToExport = Object.entries(allComponents)
239
145
  .map(
240
146
  ([component, filePath]) =>
241
147
  ` '${component}': lazy(() => import('${filePath}').then((module) => ({ default: module['${component}'] })))`,
242
148
  )
243
149
  .join(",\n");
244
150
 
245
- return `import { lazy } from 'react';
151
+ const lazyFileContent = `import { lazy } from 'react';
246
152
  export default {
247
- ${content}
153
+ ${componentsToExport}
248
154
  };
249
155
  `;
156
+
157
+ await fs.writeFile(lazyPath, lazyFileContent);
158
+
159
+ return lazyPath;
160
+ }
161
+
162
+ /**
163
+ * Ensure that the registry file does not already exist at the given path
164
+ *
165
+ * @param {Project} project - The project object
166
+ * @param {string} fileName - The name of the registry file
167
+ */
168
+ function ensureRegistryPath(project, fileName) {
169
+ const registryPath = path.join(project.root, project.distDir, fileName);
170
+ if (fs.existsSync(registryPath))
171
+ throw new Error(`A "${fileName}" file already exists at ${registryPath}.`);
172
+ return registryPath;
173
+ }
174
+
175
+ /**
176
+ * Extract a node-resolvable path
177
+ *
178
+ * @param {string} inputPath - The file path
179
+ * @returns {string} - The node-resolvable path
180
+ */
181
+ function toNodeResolvablePath(inputPath) {
182
+ const dir = path.dirname(inputPath);
183
+ const base = path.basename(inputPath, path.extname(inputPath));
184
+
185
+ return base === "index" ? dir : path.join(dir, base);
186
+ }
187
+
188
+ /**
189
+ * Create a UID from a path
190
+ *
191
+ * @param {string} inputPath - The path
192
+ * @returns {string} - The UID
193
+ */
194
+ function createUid(inputPath) {
195
+ return inputPath.replace(/[\/@\-.]/g, "_");
250
196
  }
251
197
 
252
198
  /**
package/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { DigigovCommand, resolveProject, logger } from "@digigov/cli/lib";
2
2
  import { buildFormat, generateTypeDeclarationFiles } from "./build.js";
3
- import { generateRegistryFiles } from "./generate-registry.js";
3
+ import { generateLazyRegistry, generateRegistry } from "./generate-registry.js";
4
4
  import copyFiles from "./copy-files.js";
5
5
 
6
6
  import { Option } from "commander";
@@ -55,12 +55,7 @@ async function main(options, ctx) {
55
55
  await generateTypeDeclarationFiles(project, tsconfig, ctx);
56
56
  }
57
57
 
58
- const ignore = [...TEST_GLOBS];
59
- if (options.includeStories) {
60
- logger.debug("Including stories in the build");
61
- } else {
62
- ignore.push(...STORIES_GLOBS);
63
- }
58
+ const ignore = [...TEST_GLOBS, ...STORIES_GLOBS];
64
59
  const filesToBuild = await glob(path.join(project.root, SRC_GLOB), {
65
60
  ignore,
66
61
  });
@@ -82,22 +77,39 @@ async function main(options, ctx) {
82
77
  logger.debug("Bundling done.");
83
78
 
84
79
  if (options.generateRegistry) {
85
- const registryFiles = filesToBuild.filter(
86
- (file) => !(file.includes("native") || file.endsWith(".d.ts")),
87
- );
88
80
  logger.debug("Generating registry files...");
89
- const registryFilePaths = await generateRegistryFiles(
90
- project,
91
- registryFiles,
92
- options.includeStories,
81
+
82
+ const filesToIncludeInRegistry = filesToBuild.filter(
83
+ (file) => !(file.includes("native") || file.endsWith(".d.ts")),
93
84
  );
94
- await buildFormat({
85
+ let storiesFiles = null;
86
+ if (options.includeStories) {
87
+ logger.debug("Including stories in the registry...");
88
+
89
+ storiesFiles = await glob(
90
+ STORIES_GLOBS.map((glob) => path.join(project.root, project.src, glob)),
91
+ {
92
+ ignore: ["**/*.native.*, **/*.d.ts"],
93
+ },
94
+ );
95
+ }
96
+
97
+ const [_, ...registryFilePaths] = await Promise.all([
98
+ storiesFiles
99
+ ? generateRegistry(project, storiesFiles, "stories-registry.js")
100
+ : null,
101
+ generateRegistry(project, filesToIncludeInRegistry),
102
+ generateLazyRegistry(project, filesToIncludeInRegistry),
103
+ ]);
104
+
105
+ buildFormat({
95
106
  files: registryFilePaths,
96
107
  tsconfig: tsconfig,
97
108
  format: "cjs",
98
109
  outdir: project.distDir + "/cjs",
99
- });
100
- logger.log("Generated registry files");
110
+ noLogs: true,
111
+ }),
112
+ logger.log("Generated registry files");
101
113
  }
102
114
 
103
115
  logger.debug("Copying files to build directory...");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@digigov/cli-build",
3
- "version": "2.0.0-750aec28",
3
+ "version": "2.0.0-76ec20a0",
4
4
  "description": "Build plugin for Digigov CLI",
5
5
  "main": "./index.js",
6
6
  "type": "module",
@@ -38,13 +38,13 @@
38
38
  "devDependencies": {
39
39
  "publint": "0.1.8",
40
40
  "vitest": "2.1.3",
41
- "@digigov/cli-test": "2.0.0-750aec28",
41
+ "@digigov/cli-test": "2.0.0-76ec20a0",
42
42
  "@types/fs-extra": "11.0.4",
43
43
  "@types/node": "18.19.0",
44
44
  "typescript": "5.6.2"
45
45
  },
46
46
  "peerDependencies": {
47
- "@digigov/cli": "2.0.0-750aec28",
47
+ "@digigov/cli": "2.0.0-76ec20a0",
48
48
  "next": "13.1.1"
49
49
  },
50
50
  "peerDependenciesMeta": {