summaryrefslogtreecommitdiff
path: root/scripts/cmd/build.js
diff options
context:
space:
mode:
authorGravatar Nate Moore <natemoo-re@users.noreply.github.com> 2024-03-07 12:14:48 -0600
committerGravatar GitHub <noreply@github.com> 2024-03-07 12:14:48 -0600
commit2aec2cdc21f48f9b4f1dd82e2fd16fa3d653ccc5 (patch)
tree2e15977fccc9c69f3aef884c7e76b0040df249a0 /scripts/cmd/build.js
parentbad9b583a267e239ba52237d45a89063ea277200 (diff)
downloadastro-2aec2cdc21f48f9b4f1dd82e2fd16fa3d653ccc5.tar.gz
astro-2aec2cdc21f48f9b4f1dd82e2fd16fa3d653ccc5.tar.zst
astro-2aec2cdc21f48f9b4f1dd82e2fd16fa3d653ccc5.zip
Adds `create-astro` fallback values for package versions (#10255)
* fix(create-astro): add fallback when registry fails to return the current package version * feat(create-astro): inline most current package versions as fallback * test(create-astro): update typescript tests to check for undefined * test(create-astro): properly reset fixtures * refactor: read dependencies from workspace root * refactor: error on missing values
Diffstat (limited to 'scripts/cmd/build.js')
-rw-r--r--scripts/cmd/build.js51
1 files changed, 47 insertions, 4 deletions
diff --git a/scripts/cmd/build.js b/scripts/cmd/build.js
index 5ebb66673..94645c176 100644
--- a/scripts/cmd/build.js
+++ b/scripts/cmd/build.js
@@ -55,11 +55,13 @@ export default async function build(...args) {
const {
type = 'module',
- version,
dependencies = {},
- } = await fs.readFile('./package.json').then((res) => JSON.parse(res.toString()));
- // expose PACKAGE_VERSION on process.env for CLI utils
- config.define = { 'process.env.PACKAGE_VERSION': JSON.stringify(version) };
+ } = await readPackageJSON('./package.json');
+
+ config.define = {};
+ for (const [key, value] of await getDefinedEntries()) {
+ config.define[`process.env.${key}`] = JSON.stringify(value);
+ }
const format = type === 'module' && !forceCJS ? 'esm' : 'cjs';
const outdir = 'dist';
@@ -138,3 +140,44 @@ async function clean(outdir) {
onlyFiles: true,
});
}
+
+/**
+ * Contextual `define` values to statically replace in the built JS output.
+ * Available to all packages, but mostly useful for CLIs like `create-astro`.
+ */
+async function getDefinedEntries() {
+ const define = {
+ /** The current version (at the time of building) for the current package, such as `astro` or `@astrojs/sitemap` */
+ PACKAGE_VERSION: await getInternalPackageVersion('./package.json'),
+ /** The current version (at the time of building) for `astro` */
+ ASTRO_VERSION: await getInternalPackageVersion(new URL('../../packages/astro/package.json', import.meta.url)),
+ /** The current version (at the time of building) for `@astrojs/check` */
+ ASTRO_CHECK_VERSION: await getWorkspacePackageVersion('@astrojs/check'),
+ /** The current version (at the time of building) for `typescript` */
+ TYPESCRIPT_VERSION: await getWorkspacePackageVersion('typescript'),
+ }
+ for (const [key, value] of Object.entries(define)) {
+ if (value === undefined) {
+ delete define[key];
+ }
+ }
+ return Object.entries(define);
+}
+
+async function readPackageJSON(path) {
+ return await fs.readFile(path, { encoding: 'utf8' }).then((res) => JSON.parse(res));
+}
+
+async function getInternalPackageVersion(path) {
+ return readPackageJSON(path).then(res => res.version);
+}
+
+async function getWorkspacePackageVersion(packageName) {
+ const { dependencies, devDependencies } = await readPackageJSON(new URL('../../package.json', import.meta.url));
+ const deps = { ...dependencies, ...devDependencies };
+ const version = deps[packageName];
+ if (!version) {
+ throw new Error(`Unable to resolve "${packageName}". Is it a depdendency of the workspace root?`)
+ }
+ return version.replace(/^\D+/, '');
+}