@nizza/devkit 0.1.0-alpha.1 → 0.1.0-alpha.11
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/cjs/executors/deploy/helpers.cjs +26 -0
- package/dist/cjs/executors/deploy/index.cjs +22 -0
- package/dist/cjs/executors/environments/helpers.cjs +62 -0
- package/dist/cjs/executors/environments/index.cjs +60 -0
- package/dist/cjs/executors/publish/helpers.cjs +51 -0
- package/dist/cjs/executors/publish/index.cjs +53 -0
- package/dist/cjs/executors/shared/utils.cjs +67 -0
- package/dist/cjs/executors/update-executors-file/helpers.cjs +64 -0
- package/dist/cjs/executors/update-executors-file/index.cjs +38 -0
- package/dist/cjs/executors/vtex-link/constants.cjs +9 -0
- package/dist/cjs/executors/vtex-link/helpers.cjs +206 -0
- package/dist/cjs/executors/vtex-link/index.cjs +30 -0
- package/dist/cjs/index.cjs +56 -0
- package/dist/cjs/scripts/changeset-pre-mode.cjs +41 -0
- package/dist/cjs/scripts/index.cjs +7 -0
- package/dist/cjs/shared/logger.cjs +98 -0
- package/dist/cjs/shared/utils.cjs +36 -0
- package/dist/cjs/vite-utils/config-builder/configs/app.cjs +13 -0
- package/dist/cjs/vite-utils/config-builder/configs/base.cjs +58 -0
- package/dist/cjs/vite-utils/config-builder/configs/index.cjs +36 -0
- package/dist/cjs/vite-utils/config-builder/configs/lib.cjs +57 -0
- package/dist/cjs/vite-utils/config-builder/configs/script.cjs +46 -0
- package/dist/cjs/vite-utils/config-builder/main.cjs +42 -0
- package/dist/cjs/vite-utils/config-builder/plugins.cjs +93 -0
- package/dist/cjs/vite-utils/config-builder/shared/types.cjs +33 -0
- package/dist/cjs/vite-utils/config-builder/shared/utils/helpers.cjs +115 -0
- package/dist/cjs/vite-utils/config-builder/shared/utils/projectConfig.cjs +114 -0
- package/dist/cjs/vite-utils/config-builder/shared/utils/viteCommand.cjs +59 -0
- package/dist/{src/vite/plugins/shadow-dom-inject-css/index.js → cjs/vite-utils/plugins/shadow-dom-inject-css/index.cjs} +4 -19
- package/dist/{src/vite/plugins/third-party-css-inject/main.js → cjs/vite-utils/plugins/third-party-css-inject/main.cjs} +13 -14
- package/dist/{src/vite/plugins/third-party-css-inject/utils.js → cjs/vite-utils/plugins/third-party-css-inject/utils.cjs} +9 -21
- package/dist/esm/executors/deploy/helpers.mjs +22 -0
- package/dist/esm/executors/deploy/index.mjs +20 -0
- package/dist/esm/executors/deploy/schema.json +41 -0
- package/dist/esm/executors/environments/helpers.mjs +58 -0
- package/dist/esm/executors/environments/index.mjs +58 -0
- package/dist/esm/executors/environments/schema.json +9 -0
- package/dist/esm/executors/publish/helpers.mjs +48 -0
- package/dist/esm/executors/publish/index.mjs +51 -0
- package/dist/esm/executors/publish/schema.json +19 -0
- package/dist/esm/executors/shared/utils.mjs +62 -0
- package/dist/esm/executors/update-executors-file/helpers.mjs +59 -0
- package/dist/esm/executors/update-executors-file/index.mjs +36 -0
- package/dist/esm/executors/update-executors-file/schema.json +9 -0
- package/dist/esm/executors/vtex-link/constants.mjs +5 -0
- package/dist/esm/executors/vtex-link/helpers.mjs +192 -0
- package/dist/esm/executors/vtex-link/index.mjs +28 -0
- package/dist/esm/executors/vtex-link/schema.json +25 -0
- package/dist/esm/index.mjs +12 -0
- package/dist/esm/scripts/changeset-pre-mode.mjs +39 -0
- package/dist/esm/scripts/index.mjs +1 -0
- package/dist/{src/logger.js → esm/shared/logger.mjs} +28 -39
- package/dist/esm/shared/utils.mjs +33 -0
- package/dist/esm/vite-utils/config-builder/configs/app.mjs +11 -0
- package/dist/{src/vite/config-builder/configs/base.js → esm/vite-utils/config-builder/configs/base.mjs} +23 -22
- package/dist/esm/vite-utils/config-builder/configs/index.mjs +30 -0
- package/dist/{src/vite/config-builder/configs/lib.js → esm/vite-utils/config-builder/configs/lib.mjs} +16 -18
- package/dist/esm/vite-utils/config-builder/configs/script.mjs +44 -0
- package/dist/esm/vite-utils/config-builder/main.mjs +40 -0
- package/dist/esm/vite-utils/config-builder/plugins.mjs +87 -0
- package/dist/{src/vite/config-builder/shared/types.js → esm/vite-utils/config-builder/shared/types.mjs} +2 -27
- package/dist/{src/vite/config-builder/shared/utils/helpers.d.ts → esm/vite-utils/config-builder/shared/utils/helpers.mjs} +33 -24
- package/dist/esm/vite-utils/config-builder/shared/utils/projectConfig.mjs +109 -0
- package/dist/esm/vite-utils/config-builder/shared/utils/viteCommand.mjs +56 -0
- package/dist/esm/vite-utils/plugins/shadow-dom-inject-css/index.mjs +27 -0
- package/dist/esm/vite-utils/plugins/third-party-css-inject/main.mjs +45 -0
- package/dist/esm/vite-utils/plugins/third-party-css-inject/utils.mjs +97 -0
- package/dist/index.d.ts +367 -0
- package/executors.json +29 -0
- package/package.json +52 -6
- package/dist/package.json +0 -13
- package/dist/src/executors/deploy/executor.d.ts +0 -11
- package/dist/src/executors/deploy/executor.d.ts.map +0 -1
- package/dist/src/executors/deploy/executor.js +0 -27
- package/dist/src/executors/deploy/helpers.d.ts +0 -4
- package/dist/src/executors/deploy/helpers.d.ts.map +0 -1
- package/dist/src/executors/deploy/helpers.js +0 -40
- package/dist/src/executors/environments/executor.d.ts +0 -16
- package/dist/src/executors/environments/executor.d.ts.map +0 -1
- package/dist/src/executors/environments/executor.js +0 -59
- package/dist/src/executors/environments/helpers.d.ts +0 -21
- package/dist/src/executors/environments/helpers.d.ts.map +0 -1
- package/dist/src/executors/environments/helpers.js +0 -63
- package/dist/src/executors/publish/executor.d.ts +0 -12
- package/dist/src/executors/publish/executor.d.ts.map +0 -1
- package/dist/src/executors/publish/executor.js +0 -58
- package/dist/src/executors/publish/helpers.d.ts +0 -19
- package/dist/src/executors/publish/helpers.d.ts.map +0 -1
- package/dist/src/executors/publish/helpers.js +0 -52
- package/dist/src/executors/shared/index.d.ts +0 -3
- package/dist/src/executors/shared/index.d.ts.map +0 -1
- package/dist/src/executors/shared/index.js +0 -7
- package/dist/src/executors/shared/types.d.ts +0 -4
- package/dist/src/executors/shared/types.d.ts.map +0 -1
- package/dist/src/executors/shared/types.js +0 -4
- package/dist/src/executors/shared/utils.d.ts +0 -34
- package/dist/src/executors/shared/utils.d.ts.map +0 -1
- package/dist/src/executors/shared/utils.js +0 -74
- package/dist/src/executors/update-executors-file/executor.d.ts +0 -12
- package/dist/src/executors/update-executors-file/executor.d.ts.map +0 -1
- package/dist/src/executors/update-executors-file/executor.js +0 -46
- package/dist/src/executors/update-executors-file/helpers.d.ts +0 -24
- package/dist/src/executors/update-executors-file/helpers.d.ts.map +0 -1
- package/dist/src/executors/update-executors-file/helpers.js +0 -70
- package/dist/src/executors/update-executors-file/types.d.ts +0 -14
- package/dist/src/executors/update-executors-file/types.d.ts.map +0 -1
- package/dist/src/executors/update-executors-file/types.js +0 -4
- package/dist/src/executors/vtex-link/constants.d.ts +0 -4
- package/dist/src/executors/vtex-link/constants.d.ts.map +0 -1
- package/dist/src/executors/vtex-link/constants.js +0 -24
- package/dist/src/executors/vtex-link/executor.d.ts +0 -5
- package/dist/src/executors/vtex-link/executor.d.ts.map +0 -1
- package/dist/src/executors/vtex-link/executor.js +0 -35
- package/dist/src/executors/vtex-link/helpers.d.ts +0 -77
- package/dist/src/executors/vtex-link/helpers.d.ts.map +0 -1
- package/dist/src/executors/vtex-link/helpers.js +0 -187
- package/dist/src/executors/vtex-link/types.d.ts +0 -12
- package/dist/src/executors/vtex-link/types.d.ts.map +0 -1
- package/dist/src/executors/vtex-link/types.js +0 -4
- package/dist/src/index.d.ts +0 -2
- package/dist/src/index.d.ts.map +0 -1
- package/dist/src/index.js +0 -6
- package/dist/src/logger.d.ts +0 -34
- package/dist/src/logger.d.ts.map +0 -1
- package/dist/src/scripts/changeset-pre-mode.d.ts +0 -2
- package/dist/src/scripts/changeset-pre-mode.d.ts.map +0 -1
- package/dist/src/scripts/changeset-pre-mode.js +0 -41
- package/dist/src/shared/index.d.ts +0 -2
- package/dist/src/shared/index.d.ts.map +0 -1
- package/dist/src/shared/index.js +0 -6
- package/dist/src/shared/utils.d.ts +0 -11
- package/dist/src/shared/utils.d.ts.map +0 -1
- package/dist/src/shared/utils.js +0 -22
- package/dist/src/vite/config-builder/configs/app.d.ts +0 -4
- package/dist/src/vite/config-builder/configs/app.d.ts.map +0 -1
- package/dist/src/vite/config-builder/configs/app.js +0 -19
- package/dist/src/vite/config-builder/configs/base.d.ts +0 -4
- package/dist/src/vite/config-builder/configs/base.d.ts.map +0 -1
- package/dist/src/vite/config-builder/configs/index.d.ts +0 -7
- package/dist/src/vite/config-builder/configs/index.d.ts.map +0 -1
- package/dist/src/vite/config-builder/configs/index.js +0 -28
- package/dist/src/vite/config-builder/configs/lib.d.ts +0 -4
- package/dist/src/vite/config-builder/configs/lib.d.ts.map +0 -1
- package/dist/src/vite/config-builder/configs/script.d.ts +0 -4
- package/dist/src/vite/config-builder/configs/script.d.ts.map +0 -1
- package/dist/src/vite/config-builder/configs/script.js +0 -42
- package/dist/src/vite/config-builder/index.d.ts +0 -3
- package/dist/src/vite/config-builder/index.d.ts.map +0 -1
- package/dist/src/vite/config-builder/index.js +0 -7
- package/dist/src/vite/config-builder/main.d.ts +0 -10
- package/dist/src/vite/config-builder/main.d.ts.map +0 -1
- package/dist/src/vite/config-builder/main.js +0 -37
- package/dist/src/vite/config-builder/plugins.d.ts +0 -30
- package/dist/src/vite/config-builder/plugins.d.ts.map +0 -1
- package/dist/src/vite/config-builder/plugins.js +0 -88
- package/dist/src/vite/config-builder/shared/index.d.ts +0 -3
- package/dist/src/vite/config-builder/shared/index.d.ts.map +0 -1
- package/dist/src/vite/config-builder/shared/index.js +0 -7
- package/dist/src/vite/config-builder/shared/types.d.ts +0 -155
- package/dist/src/vite/config-builder/shared/types.d.ts.map +0 -1
- package/dist/src/vite/config-builder/shared/utils/helpers.d.ts.map +0 -1
- package/dist/src/vite/config-builder/shared/utils/helpers.js +0 -61
- package/dist/src/vite/config-builder/shared/utils/index.d.ts +0 -4
- package/dist/src/vite/config-builder/shared/utils/index.d.ts.map +0 -1
- package/dist/src/vite/config-builder/shared/utils/index.js +0 -8
- package/dist/src/vite/config-builder/shared/utils/projectConfig.d.ts +0 -60
- package/dist/src/vite/config-builder/shared/utils/projectConfig.d.ts.map +0 -1
- package/dist/src/vite/config-builder/shared/utils/projectConfig.js +0 -85
- package/dist/src/vite/config-builder/shared/utils/viteCommand.d.ts +0 -24
- package/dist/src/vite/config-builder/shared/utils/viteCommand.d.ts.map +0 -1
- package/dist/src/vite/config-builder/shared/utils/viteCommand.js +0 -54
- package/dist/src/vite/index.d.ts +0 -3
- package/dist/src/vite/index.d.ts.map +0 -1
- package/dist/src/vite/index.js +0 -7
- package/dist/src/vite/plugins/index.d.ts +0 -3
- package/dist/src/vite/plugins/index.d.ts.map +0 -1
- package/dist/src/vite/plugins/index.js +0 -7
- package/dist/src/vite/plugins/shadow-dom-inject-css/index.d.ts +0 -8
- package/dist/src/vite/plugins/shadow-dom-inject-css/index.d.ts.map +0 -1
- package/dist/src/vite/plugins/third-party-css-inject/index.d.ts +0 -4
- package/dist/src/vite/plugins/third-party-css-inject/index.d.ts.map +0 -1
- package/dist/src/vite/plugins/third-party-css-inject/index.js +0 -13
- package/dist/src/vite/plugins/third-party-css-inject/main.d.ts +0 -17
- package/dist/src/vite/plugins/third-party-css-inject/main.d.ts.map +0 -1
- package/dist/src/vite/plugins/third-party-css-inject/utils.d.ts +0 -5
- package/dist/src/vite/plugins/third-party-css-inject/utils.d.ts.map +0 -1
- /package/dist/{src → cjs}/executors/deploy/schema.json +0 -0
- /package/dist/{src → cjs}/executors/environments/schema.json +0 -0
- /package/dist/{src → cjs}/executors/publish/schema.json +0 -0
- /package/dist/{src → cjs}/executors/update-executors-file/schema.json +0 -0
- /package/dist/{src → cjs}/executors/vtex-link/schema.json +0 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var devkit = require('@nx/devkit');
|
|
4
|
+
var utils = require('../shared/utils.cjs');
|
|
5
|
+
|
|
6
|
+
async function cleanS3Bucket(s3BucketPath) {
|
|
7
|
+
devkit.logger.info('🧹 Cleaning S3 bucket...');
|
|
8
|
+
await utils.runCommand(`aws s3 rm ${s3BucketPath} --recursive`);
|
|
9
|
+
devkit.logger.info('✅ S3 bucket cleared.');
|
|
10
|
+
}
|
|
11
|
+
async function copyAssetsToS3(s3BucketPath, projectRoot) {
|
|
12
|
+
devkit.logger.info('📦 Copying assets to S3 bucket...');
|
|
13
|
+
await utils.runCommand(`aws s3 cp ${projectRoot}/dist ${s3BucketPath} --recursive`);
|
|
14
|
+
devkit.logger.info('🚚 Assets files copied.');
|
|
15
|
+
}
|
|
16
|
+
async function createCloudFrontInvalidation(distributionId, invalidationPaths) {
|
|
17
|
+
const pathsString = invalidationPaths.map((path)=>`"${path}"`).join(' ');
|
|
18
|
+
const command = `aws cloudfront create-invalidation --distribution-id ${distributionId} --paths ${pathsString}`;
|
|
19
|
+
devkit.logger.info('🔃 Creating invalidation for CloudFront...');
|
|
20
|
+
await utils.runCommand(command);
|
|
21
|
+
devkit.logger.info('🔄 CloudFront invalidation created.');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
exports.cleanS3Bucket = cleanS3Bucket;
|
|
25
|
+
exports.copyAssetsToS3 = copyAssetsToS3;
|
|
26
|
+
exports.createCloudFrontInvalidation = createCloudFrontInvalidation;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var utils = require('../shared/utils.cjs');
|
|
4
|
+
var helpers = require('./helpers.cjs');
|
|
5
|
+
|
|
6
|
+
async function deployExecutor(opts, ctx) {
|
|
7
|
+
try {
|
|
8
|
+
const { root, name } = utils.getProjectConfig(ctx);
|
|
9
|
+
const { s3BucketPath, cloudFrontDistributionId, cloudFrontInvalidationPaths, skipBuild = false } = opts;
|
|
10
|
+
const buildTarget = `${name}:${opts.buildTarget}`;
|
|
11
|
+
if (!skipBuild) await utils.runCommand(`nx run ${buildTarget}`);
|
|
12
|
+
await helpers.cleanS3Bucket(s3BucketPath);
|
|
13
|
+
await helpers.copyAssetsToS3(s3BucketPath, root);
|
|
14
|
+
await helpers.createCloudFrontInvalidation(cloudFrontDistributionId, cloudFrontInvalidationPaths);
|
|
15
|
+
const now = new Date().toLocaleString();
|
|
16
|
+
return utils.ok(`✨ Build completed successfully on ${now}`);
|
|
17
|
+
} catch (error) {
|
|
18
|
+
return utils.err(error, '❌ Error when executing deploy command');
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
module.exports = deployExecutor;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var fs = require('fs');
|
|
4
|
+
var path = require('path');
|
|
5
|
+
var devkit = require('@nx/devkit');
|
|
6
|
+
|
|
7
|
+
const { mkdir, writeFile, access } = fs.promises;
|
|
8
|
+
/**
|
|
9
|
+
* Creates the content for an environment file based on the core environment path.
|
|
10
|
+
*
|
|
11
|
+
* @param {CreateEnvironmentData} data - Values to replace in template.
|
|
12
|
+
* @returns {string} The content of the environment file.
|
|
13
|
+
*/ async function createEnvironmentFileContent({ coreEnvFile, projectOrg }) {
|
|
14
|
+
return `import { Environment } from '@${projectOrg}/core';
|
|
15
|
+
import coreEnvs from '@${projectOrg}/core/envs/${coreEnvFile}';
|
|
16
|
+
|
|
17
|
+
const config = {
|
|
18
|
+
...coreEnvs,
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export const environment: Environment<typeof config> = config;
|
|
22
|
+
export default environment;
|
|
23
|
+
`;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Ensures that the given directory exists, creating it if it does not.
|
|
27
|
+
*
|
|
28
|
+
* @param {string} dir - The path to the directory to check/create.
|
|
29
|
+
*/ async function ensureDirectoryExists(dir) {
|
|
30
|
+
try {
|
|
31
|
+
await access(dir);
|
|
32
|
+
devkit.logger.info('✅ Directory exists.');
|
|
33
|
+
} catch {
|
|
34
|
+
devkit.logger.info('📁 Directory does not exist. Creating...');
|
|
35
|
+
await mkdir(dir, {
|
|
36
|
+
recursive: true
|
|
37
|
+
});
|
|
38
|
+
devkit.logger.info('📁 Directory created.');
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Writes environment files to the given base path if they do not already exist.
|
|
43
|
+
*
|
|
44
|
+
* @param {string} basePath - The base directory where the files should be written.
|
|
45
|
+
* @param {Record<string, string>} files - An object containing filenames and their respective content.
|
|
46
|
+
*/ async function writeFilesIfNotExist(basePath, files) {
|
|
47
|
+
await Promise.all(Object.entries(files).map(async ([filename, content])=>{
|
|
48
|
+
const filePath = path.join(basePath, filename);
|
|
49
|
+
try {
|
|
50
|
+
await access(filePath);
|
|
51
|
+
devkit.logger.info(`✅ The file "${filename}" already exists.`);
|
|
52
|
+
} catch {
|
|
53
|
+
devkit.logger.warn(`📝 The file "${filename}" does not exist. Creating...`);
|
|
54
|
+
await writeFile(filePath, content);
|
|
55
|
+
devkit.logger.info(`🎉 The file "${filename}" has been created.`);
|
|
56
|
+
}
|
|
57
|
+
}));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
exports.createEnvironmentFileContent = createEnvironmentFileContent;
|
|
61
|
+
exports.ensureDirectoryExists = ensureDirectoryExists;
|
|
62
|
+
exports.writeFilesIfNotExist = writeFilesIfNotExist;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var path = require('path');
|
|
4
|
+
var utils = require('../shared/utils.cjs');
|
|
5
|
+
var helpers = require('./helpers.cjs');
|
|
6
|
+
require('../../shared/logger.cjs');
|
|
7
|
+
var utils$1 = require('../../shared/utils.cjs');
|
|
8
|
+
|
|
9
|
+
const ENV_DIR = 'src/environments';
|
|
10
|
+
const ENV_DEV_FILE = 'environment.ts';
|
|
11
|
+
const ENV_LOCAL_FILE = 'environment.local.ts';
|
|
12
|
+
const ENV_PROD_FILE = 'environment.prod.ts';
|
|
13
|
+
const ENV_STAGING_FILE = 'environment.staging.ts';
|
|
14
|
+
const environmentFiles = [
|
|
15
|
+
ENV_DEV_FILE,
|
|
16
|
+
ENV_LOCAL_FILE,
|
|
17
|
+
ENV_PROD_FILE,
|
|
18
|
+
ENV_STAGING_FILE
|
|
19
|
+
];
|
|
20
|
+
/**
|
|
21
|
+
* The executor function to create environment files.
|
|
22
|
+
* Ensures that the directory for the environment files exists and creates the files
|
|
23
|
+
* if they do not exist already. Files created are 'environment.ts', 'environment.prod.ts',
|
|
24
|
+
* and 'environment.staging.ts'.
|
|
25
|
+
*
|
|
26
|
+
* @param {ExecutorOptions} _opts - Executor options, currently not used but provided for future extensibility.
|
|
27
|
+
* @param {ExecutorContext} ctx - The executor context, providing access to the project configuration.
|
|
28
|
+
* @returns {Promise<ExecutorResponse>} The result object with a success or error status.
|
|
29
|
+
*/ async function runExecutor(_opts, ctx) {
|
|
30
|
+
try {
|
|
31
|
+
const projectConfig = utils.getProjectConfig(ctx);
|
|
32
|
+
const environmentsDir = path.join(projectConfig.root, ENV_DIR);
|
|
33
|
+
const mainPkgJson = utils$1.getPkgJson(ctx.root);
|
|
34
|
+
const getEnvPath = (fileName)=>{
|
|
35
|
+
const isLocal = fileName.includes('.local');
|
|
36
|
+
const coreEnvFile = (isLocal ? ENV_DEV_FILE : fileName).replace('.ts', '');
|
|
37
|
+
const key = isLocal ? ENV_LOCAL_FILE : fileName;
|
|
38
|
+
return {
|
|
39
|
+
key,
|
|
40
|
+
coreEnvFile
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
await helpers.ensureDirectoryExists(environmentsDir);
|
|
44
|
+
for (const envFileName of environmentFiles){
|
|
45
|
+
const { key, coreEnvFile } = getEnvPath(envFileName);
|
|
46
|
+
const content = await helpers.createEnvironmentFileContent({
|
|
47
|
+
coreEnvFile,
|
|
48
|
+
projectOrg: mainPkgJson.name
|
|
49
|
+
});
|
|
50
|
+
await helpers.writeFilesIfNotExist(environmentsDir, {
|
|
51
|
+
[key]: content
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
return utils.ok();
|
|
55
|
+
} catch (error) {
|
|
56
|
+
return utils.err(error, `Failed to create environment files:`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
module.exports = runExecutor;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var devkit = require('@nx/devkit');
|
|
4
|
+
var child_process = require('child_process');
|
|
5
|
+
var util = require('util');
|
|
6
|
+
|
|
7
|
+
const execAsync = util.promisify(child_process.exec);
|
|
8
|
+
/**
|
|
9
|
+
* Retrieves the version of a published package with a given name and tag.
|
|
10
|
+
*
|
|
11
|
+
* @param {string} packageName The name of the package to retrieve the version for.
|
|
12
|
+
* @param {string} tag The tag to retrieve the version for.
|
|
13
|
+
* @returns The version of the package as a string or an object containing versions for each tag.
|
|
14
|
+
*/ async function getPublishedPackageVersion(packageName, tag) {
|
|
15
|
+
const withTag = tag ? `.${tag}` : ' --json';
|
|
16
|
+
const command = `npm view ${packageName} dist-tags${withTag}`;
|
|
17
|
+
const { stdout, stderr } = await execAsync(command);
|
|
18
|
+
if (stderr) {
|
|
19
|
+
devkit.logger.error(stderr.trim());
|
|
20
|
+
process.exit(1);
|
|
21
|
+
}
|
|
22
|
+
const published = stdout.trim();
|
|
23
|
+
return tag ? published : JSON.parse(published);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Checks if a package with the given name is already published and returns its version.
|
|
27
|
+
*
|
|
28
|
+
* @param {string} packageName The name of the package to check for.
|
|
29
|
+
* @returns An object containing the published versions of the package and a flag indicating if it is the first version.
|
|
30
|
+
*/ async function checkPublishedVersion(packageName) {
|
|
31
|
+
let publishedVersion;
|
|
32
|
+
let isFirstVersion = false;
|
|
33
|
+
try {
|
|
34
|
+
publishedVersion = await getPublishedPackageVersion(packageName);
|
|
35
|
+
} catch (err) {
|
|
36
|
+
if (err?.message.includes('code E404')) {
|
|
37
|
+
isFirstVersion = true;
|
|
38
|
+
devkit.logger.info(`🏁 This is the first version of "${packageName}" package. \n`);
|
|
39
|
+
} else {
|
|
40
|
+
devkit.logger.error('❌ Error when checking the last published versions \n');
|
|
41
|
+
throw err;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
publishedVersion,
|
|
46
|
+
isFirstVersion
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
exports.checkPublishedVersion = checkPublishedVersion;
|
|
51
|
+
exports.getPublishedPackageVersion = getPublishedPackageVersion;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var devkit = require('@nx/devkit');
|
|
4
|
+
var semver = require('semver');
|
|
5
|
+
require('../../shared/logger.cjs');
|
|
6
|
+
var utils$1 = require('../../shared/utils.cjs');
|
|
7
|
+
var utils = require('../shared/utils.cjs');
|
|
8
|
+
var helpers = require('./helpers.cjs');
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* This is a minimal script to publish package to "npm".
|
|
12
|
+
* Need to authenticate with NPM before running this script.
|
|
13
|
+
*/ async function runExecutor(opts, ctx) {
|
|
14
|
+
try {
|
|
15
|
+
const { tag, dryRun: isDryRun } = opts;
|
|
16
|
+
const { root: projectRoot } = utils.getProjectConfig(ctx);
|
|
17
|
+
// Read the package.json file and extract the name and version.
|
|
18
|
+
const packageJson = utils$1.getPkgJson(projectRoot);
|
|
19
|
+
const { name: packageName, version: packageVersion } = packageJson;
|
|
20
|
+
// Check if the package has been previously published and get the published version.
|
|
21
|
+
const { publishedVersion, isFirstVersion } = await helpers.checkPublishedVersion(packageName);
|
|
22
|
+
let greaterThanTagVersion, greaterThanLatestVersion;
|
|
23
|
+
if (!isFirstVersion) {
|
|
24
|
+
// Check if the package version is greater than the latest published version for the specified tag.
|
|
25
|
+
greaterThanTagVersion = publishedVersion?.[tag] ? semver.gt(packageVersion, publishedVersion?.[tag]) : true;
|
|
26
|
+
// Check if the package version is greater than the latest published version.
|
|
27
|
+
greaterThanLatestVersion = semver.gt(packageVersion, publishedVersion.latest);
|
|
28
|
+
console.log(`🔎 package.json version: ${packageVersion}`);
|
|
29
|
+
console.log('🔺 published versions:');
|
|
30
|
+
Object.entries(publishedVersion).forEach(([k, v])=>console.log(` * ${k}: ${v}`));
|
|
31
|
+
console.log('\n');
|
|
32
|
+
}
|
|
33
|
+
// Publish the package if it's the first version or if it's a new version.
|
|
34
|
+
const needsPublish = isFirstVersion || greaterThanLatestVersion && greaterThanTagVersion;
|
|
35
|
+
if (needsPublish) {
|
|
36
|
+
const dryRun = isDryRun ? '--dry-run' : '';
|
|
37
|
+
const commandToPublish = `pnpm publish --access public --tag ${tag} --no-git-checks ${dryRun}`;
|
|
38
|
+
isDryRun && devkit.logger.info('👀 Skip publish (dry run) \n');
|
|
39
|
+
devkit.logger.info(`${commandToPublish} \n`);
|
|
40
|
+
// Execute the command to publish the package.
|
|
41
|
+
// process.chdir(projectRoot);
|
|
42
|
+
await utils.runCommand(commandToPublish, projectRoot);
|
|
43
|
+
devkit.logger.info(`\n🚀 Successfully published ${packageName}@${packageVersion}`);
|
|
44
|
+
} else {
|
|
45
|
+
devkit.logger.warn(`👻 Package version "${packageVersion}" is invalid.`);
|
|
46
|
+
}
|
|
47
|
+
return utils.ok();
|
|
48
|
+
} catch (error) {
|
|
49
|
+
return utils.err(error, '❌ Error when publishing the package');
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
module.exports = runExecutor;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var devkit = require('@nx/devkit');
|
|
4
|
+
var child_process = require('child_process');
|
|
5
|
+
var util = require('util');
|
|
6
|
+
|
|
7
|
+
const execAsync = util.promisify(child_process.exec);
|
|
8
|
+
/**
|
|
9
|
+
* Indicates a successful executor operation.
|
|
10
|
+
*
|
|
11
|
+
* @param {string} message - Optional message to log for the success case.
|
|
12
|
+
* @returns {ExecutorResponse} - An object representing a successful operation.
|
|
13
|
+
*/ function ok(message) {
|
|
14
|
+
message && devkit.logger.info(message);
|
|
15
|
+
return {
|
|
16
|
+
success: true
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Indicates a failed executor operation.
|
|
21
|
+
*
|
|
22
|
+
* @param {string | Error | unknown} err - An optional error object or message indicating the nature of the failure.
|
|
23
|
+
* @param {string} message - An optional message to log for the error case. If not provided, a default error message is logged.
|
|
24
|
+
* @returns {ExecutorResponse} - An object representing a failed operation.
|
|
25
|
+
*/ function err(err, message) {
|
|
26
|
+
if (message) {
|
|
27
|
+
devkit.logger.error(message);
|
|
28
|
+
} else {
|
|
29
|
+
devkit.logger.error(`❌ an error occurred:`);
|
|
30
|
+
}
|
|
31
|
+
if (err) devkit.logger.error(err);
|
|
32
|
+
return {
|
|
33
|
+
success: false
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Retrieves the project configuration for the current execution context.
|
|
38
|
+
*
|
|
39
|
+
* @param {ExecutorContext} ctx - The execution context containing project information.
|
|
40
|
+
* @returns {ProjectConfiguration} - The configuration object for the current project.
|
|
41
|
+
* @throws {Error} If the project configuration is invalid or not found.
|
|
42
|
+
*/ function getProjectConfig(ctx) {
|
|
43
|
+
const projectName = ctx.projectName || '';
|
|
44
|
+
const projectConfig = ctx.projectsConfigurations?.projects?.[projectName];
|
|
45
|
+
if (!projectConfig) throw new Error('Invalid project configuration.');
|
|
46
|
+
return projectConfig;
|
|
47
|
+
}
|
|
48
|
+
async function runCommand(command, cwd) {
|
|
49
|
+
try {
|
|
50
|
+
const { stdout, stderr } = await execAsync(command, {
|
|
51
|
+
...cwd && {
|
|
52
|
+
cwd
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
devkit.logger.info(stdout);
|
|
56
|
+
devkit.logger.info(stderr);
|
|
57
|
+
} catch (error) {
|
|
58
|
+
devkit.logger.error(`Unexpected error running command:`);
|
|
59
|
+
devkit.logger.error(error);
|
|
60
|
+
throw error;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
exports.err = err;
|
|
65
|
+
exports.getProjectConfig = getProjectConfig;
|
|
66
|
+
exports.ok = ok;
|
|
67
|
+
exports.runCommand = runCommand;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var devkit = require('@nx/devkit');
|
|
4
|
+
var fs = require('fs');
|
|
5
|
+
var R = require('ramda');
|
|
6
|
+
|
|
7
|
+
const { writeFile } = fs.promises;
|
|
8
|
+
const transformPathToRoot = (path)=>{
|
|
9
|
+
const distPrefix = './dist/cjs/';
|
|
10
|
+
const srcRegex = /^\.\/src\//;
|
|
11
|
+
const executorTsRegex = /executor\.ts$/;
|
|
12
|
+
const jsonFileRegex = /\.json$/;
|
|
13
|
+
const isJson = jsonFileRegex.test(path);
|
|
14
|
+
const changeSrcPath = (p)=>p.replace(srcRegex, distPrefix);
|
|
15
|
+
const changeFileName = (p)=>isJson ? p : p.replace(executorTsRegex, 'index.cjs');
|
|
16
|
+
return R.pipe(changeSrcPath, changeFileName)(path);
|
|
17
|
+
};
|
|
18
|
+
const transformPathToDist = (path)=>{
|
|
19
|
+
const cjsPrefix = './cjs/';
|
|
20
|
+
const distRegex = /^\.\/dist\/cjs\//;
|
|
21
|
+
return path.replace(distRegex, cjsPrefix);
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Updates the executor paths for the provided executors object.
|
|
25
|
+
* @param {Executors} executors - The executors object from executors.json.
|
|
26
|
+
* @param {boolean} toDist - Flag to determine if './dist' should be removed from paths.
|
|
27
|
+
* @returns - The updated executors object with transformed paths.
|
|
28
|
+
*/ const updateExecutorPaths = (executors, toDist)=>{
|
|
29
|
+
const transform = R.cond([
|
|
30
|
+
[
|
|
31
|
+
R.always(toDist),
|
|
32
|
+
transformPathToDist
|
|
33
|
+
],
|
|
34
|
+
[
|
|
35
|
+
R.T,
|
|
36
|
+
transformPathToRoot
|
|
37
|
+
]
|
|
38
|
+
]);
|
|
39
|
+
return Object.entries(executors).reduce((updatedExecutors, [key, executor])=>{
|
|
40
|
+
updatedExecutors[key] = {
|
|
41
|
+
...executor,
|
|
42
|
+
implementation: transform(executor.implementation),
|
|
43
|
+
schema: transform(executor.schema)
|
|
44
|
+
};
|
|
45
|
+
return updatedExecutors;
|
|
46
|
+
}, {});
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Writes a JSON file asynchronously.
|
|
50
|
+
* @param {string} path - The file path where to write the JSON data.
|
|
51
|
+
* @param {any} data - The data to be written to the file.
|
|
52
|
+
*/ const writeJsonFileAsync = async (path, data)=>{
|
|
53
|
+
try {
|
|
54
|
+
await writeFile(path, JSON.stringify(data, null, 2));
|
|
55
|
+
devkit.logger.info(`✅ Updated: ${path}`);
|
|
56
|
+
} catch (error) {
|
|
57
|
+
devkit.logger.error(`❌ Error writing file ${path}: ${error}`);
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
exports.transformPathToDist = transformPathToDist;
|
|
62
|
+
exports.transformPathToRoot = transformPathToRoot;
|
|
63
|
+
exports.updateExecutorPaths = updateExecutorPaths;
|
|
64
|
+
exports.writeJsonFileAsync = writeJsonFileAsync;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var fs = require('fs');
|
|
4
|
+
var path = require('path');
|
|
5
|
+
var utils = require('../shared/utils.cjs');
|
|
6
|
+
var helpers = require('./helpers.cjs');
|
|
7
|
+
|
|
8
|
+
const { readFile } = fs.promises;
|
|
9
|
+
/**
|
|
10
|
+
* Executor function to update paths in the executors.json file.
|
|
11
|
+
*
|
|
12
|
+
* @param {ExecutorOptions} _opts - Executor options, currently not used but provided for future extensibility.
|
|
13
|
+
* @param {ExecutorContext} ctx - The executor context, providing access to the project configuration.
|
|
14
|
+
* @returns {Promise<ExecutorResponse>} The result object with a success or error status.
|
|
15
|
+
*/ async function runExecutor(_opts, ctx) {
|
|
16
|
+
try {
|
|
17
|
+
const projectConfig = utils.getProjectConfig(ctx);
|
|
18
|
+
const rootDir = projectConfig.root;
|
|
19
|
+
const executorsFilePath = path.join(rootDir, 'executors.json');
|
|
20
|
+
const distExecutorsFilePath = path.join(rootDir, 'dist', 'executors.json');
|
|
21
|
+
const originalContent = JSON.parse(await readFile(executorsFilePath, 'utf8'));
|
|
22
|
+
// Update paths to point to './dist/' and add '.js'
|
|
23
|
+
const updatedExecutors = helpers.updateExecutorPaths(originalContent.executors, false);
|
|
24
|
+
// Prepare the version for the dist folder by removing './dist/' from paths
|
|
25
|
+
// const distExecutors = updateExecutorPaths(updatedExecutors, true);
|
|
26
|
+
// Write both files asynchronously and in parallel
|
|
27
|
+
await Promise.all([
|
|
28
|
+
helpers.writeJsonFileAsync(executorsFilePath, {
|
|
29
|
+
executors: updatedExecutors
|
|
30
|
+
})
|
|
31
|
+
]);
|
|
32
|
+
return utils.ok(`🚀 Executors updated successfully.`);
|
|
33
|
+
} catch (error) {
|
|
34
|
+
return utils.err(error, `❌ Error processing file: ${error}`);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
module.exports = runExecutor;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const VTEX_PROJECT_PATH = process.env.NZ_VTEX_PROJECT;
|
|
4
|
+
const ERROR_LINKING_FLAG = '[31merror[39m: App build failed';
|
|
5
|
+
const SUCCESS_LINKING_FLAG = '[32minfo[39m: App running';
|
|
6
|
+
|
|
7
|
+
exports.ERROR_LINKING_FLAG = ERROR_LINKING_FLAG;
|
|
8
|
+
exports.SUCCESS_LINKING_FLAG = SUCCESS_LINKING_FLAG;
|
|
9
|
+
exports.VTEX_PROJECT_PATH = VTEX_PROJECT_PATH;
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var devkit = require('@nx/devkit');
|
|
4
|
+
var child_process = require('child_process');
|
|
5
|
+
var fs = require('fs');
|
|
6
|
+
var path = require('path');
|
|
7
|
+
var util = require('util');
|
|
8
|
+
require('../../shared/logger.cjs');
|
|
9
|
+
var utils$1 = require('../../shared/utils.cjs');
|
|
10
|
+
var utils = require('../shared/utils.cjs');
|
|
11
|
+
var constants = require('./constants.cjs');
|
|
12
|
+
|
|
13
|
+
const execAsync = util.promisify(child_process.exec);
|
|
14
|
+
const getPkgJsonPath = (vtexProjectPath)=>path.resolve(vtexProjectPath, 'package.json');
|
|
15
|
+
/**
|
|
16
|
+
* Links the local package to the VTEX environment.
|
|
17
|
+
*
|
|
18
|
+
* @param {SharedContext} ctx - The shared context containing paths and configurations.
|
|
19
|
+
* @returns {Promise<void>} A promise that resolves when the package is linked or rejects on error.
|
|
20
|
+
*/ function vtexLink(ctx) {
|
|
21
|
+
const { vtexProjectPath } = ctx;
|
|
22
|
+
return new Promise((resolve, reject)=>{
|
|
23
|
+
devkit.logger.info('🔗 Linking package to VTEX...');
|
|
24
|
+
const link = child_process.spawn('vtex', [
|
|
25
|
+
'link',
|
|
26
|
+
'-u'
|
|
27
|
+
], {
|
|
28
|
+
cwd: vtexProjectPath,
|
|
29
|
+
detached: true
|
|
30
|
+
});
|
|
31
|
+
link.stdout.on('data', (chunk)=>{
|
|
32
|
+
const data = chunk.toString('utf8');
|
|
33
|
+
if (data.includes(constants.ERROR_LINKING_FLAG)) {
|
|
34
|
+
reject('could not link package in VTEX');
|
|
35
|
+
}
|
|
36
|
+
if (data.includes(constants.SUCCESS_LINKING_FLAG)) {
|
|
37
|
+
link.pid && process.kill(-link.pid);
|
|
38
|
+
setTimeout(()=>{
|
|
39
|
+
link.kill();
|
|
40
|
+
devkit.logger.info('✅ Successfully linked package to VTEX.');
|
|
41
|
+
}, 200);
|
|
42
|
+
resolve();
|
|
43
|
+
}
|
|
44
|
+
devkit.logger.info(data);
|
|
45
|
+
});
|
|
46
|
+
link.on('error', (error)=>reject(error.message));
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Updates the package.json file within the VTEX project to use the local version of the package.
|
|
51
|
+
*
|
|
52
|
+
* @param {SharedContext} ctx - The shared context containing paths and configurations.
|
|
53
|
+
* @param {string} packageName - The name of the package to link.
|
|
54
|
+
* @returns {Promise<void>} A promise that resolves when the package.json is updated.
|
|
55
|
+
*/ async function changePackageJsonFromVtexProject(ctx, packageName) {
|
|
56
|
+
const { vtexProjectPath, projectName, packageOrg } = ctx;
|
|
57
|
+
const pkgJsonPath = getPkgJsonPath(vtexProjectPath);
|
|
58
|
+
// Make backup of package.json
|
|
59
|
+
await fs.promises.copyFile(pkgJsonPath, path.join(vtexProjectPath, 'package.json.nxback'));
|
|
60
|
+
devkit.logger.info('🔃 Created package.json backup.');
|
|
61
|
+
// Find and edit value "@${packageOrg}/${projectName}" dependency
|
|
62
|
+
const packageJson = utils$1.getPkgJson(vtexProjectPath);
|
|
63
|
+
const newPackageJson = {
|
|
64
|
+
...packageJson,
|
|
65
|
+
dependencies: {
|
|
66
|
+
...packageJson.dependencies,
|
|
67
|
+
[`@${packageOrg}/${projectName}`]: `file:./${packageName}`
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
await fs.promises.unlink(pkgJsonPath);
|
|
71
|
+
await fs.promises.writeFile(pkgJsonPath, JSON.stringify(newPackageJson, null, 2));
|
|
72
|
+
devkit.logger.info('📝 Updated package.json in VTEX project.');
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Restores the original package.json from the backup created before modifications.
|
|
76
|
+
*
|
|
77
|
+
* @param {SharedContext} ctx - The shared context containing paths and configurations.
|
|
78
|
+
* @returns {Promise<void>} A promise that resolves when the package.json is restored.
|
|
79
|
+
*/ async function restorePackageJsonFromVtexProject(ctx) {
|
|
80
|
+
const { vtexProjectPath } = ctx;
|
|
81
|
+
const pkgJsonPath = getPkgJsonPath(vtexProjectPath);
|
|
82
|
+
const backupFile = path.resolve(vtexProjectPath, 'package.json.nxback');
|
|
83
|
+
if (!fs.existsSync(backupFile)) return;
|
|
84
|
+
await fs.promises.unlink(pkgJsonPath);
|
|
85
|
+
await fs.promises.copyFile(backupFile, pkgJsonPath);
|
|
86
|
+
await fs.promises.unlink(backupFile);
|
|
87
|
+
devkit.logger.info('🔄 Restored package.json in VTEX project.');
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Renames the generated package file with a unique identifier.
|
|
91
|
+
*
|
|
92
|
+
* @param {SharedContext} ctx - The shared context containing paths and configurations.
|
|
93
|
+
* @returns {Promise<string>} A promise that resolves with the new name of the package.
|
|
94
|
+
*/ async function renamePackage(ctx) {
|
|
95
|
+
const { projectRoot, projectName, packageOrg } = ctx;
|
|
96
|
+
const packageJson = utils$1.getPkgJson('./');
|
|
97
|
+
const oldName = `${packageOrg}-${projectName}-${packageJson.version}.tgz`;
|
|
98
|
+
const oldPath = path.join(projectRoot, oldName);
|
|
99
|
+
const newName = `${packageOrg}-${projectName}-${packageJson.version}-${utils$1.shortId(8)}.tgz`;
|
|
100
|
+
const newPath = path.join(projectRoot, newName);
|
|
101
|
+
await fs.promises.rename(oldPath, newPath);
|
|
102
|
+
devkit.logger.info(`🏷️ New package name: ${newName}`);
|
|
103
|
+
return newName;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Creates a pnpm package for uploading to VTEX.
|
|
107
|
+
*
|
|
108
|
+
* @param {SharedContext} ctx - The shared context containing paths and configurations.
|
|
109
|
+
* @returns {Promise<string>} A promise that resolves with the name of the created package.
|
|
110
|
+
*/ async function createPackage(ctx) {
|
|
111
|
+
const command = 'pnpm pack';
|
|
112
|
+
const { stderr } = await execAsync(command);
|
|
113
|
+
if (stderr) devkit.logger.error(stderr);
|
|
114
|
+
devkit.logger.info('📦 Package generated successfully.');
|
|
115
|
+
return renamePackage(ctx);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Searches for and lists all outdated packages in the VTEX project directory.
|
|
119
|
+
*
|
|
120
|
+
* @param {SharedContext} ctx - The shared context containing paths and configurations.
|
|
121
|
+
* @returns {Promise<string[]>} A promise that resolves with a list of deprecated package names.
|
|
122
|
+
*/ async function findLastPackages(ctx) {
|
|
123
|
+
const { vtexProjectPath, projectName, packageOrg } = ctx;
|
|
124
|
+
const matchedFiles = [];
|
|
125
|
+
const files = await fs.promises.readdir(vtexProjectPath);
|
|
126
|
+
for (const file of files){
|
|
127
|
+
const regex = new RegExp(`^${packageOrg}-${projectName}-.*$`);
|
|
128
|
+
if (regex.test(file)) {
|
|
129
|
+
matchedFiles.push(file);
|
|
130
|
+
devkit.logger.info(`🔍 Found deprecated package: ${file}`);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return matchedFiles;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Removes all outdated packages in the VTEX project directory.
|
|
137
|
+
*
|
|
138
|
+
* @param {SharedContext} ctx - The shared context containing paths and configurations.
|
|
139
|
+
* @returns {Promise<void>} A promise that resolves when the old packages are removed.
|
|
140
|
+
*/ async function removeOldPackages(ctx) {
|
|
141
|
+
const { vtexProjectPath } = ctx;
|
|
142
|
+
const files = await findLastPackages(ctx);
|
|
143
|
+
const toRemove = files.map((x)=>fs.promises.unlink(path.resolve(vtexProjectPath, x)));
|
|
144
|
+
await Promise.all(toRemove);
|
|
145
|
+
devkit.logger.info('🗑️ Removed old packages in VTEX project.');
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Moves a package to the VTEX project directory.
|
|
149
|
+
*
|
|
150
|
+
* @param {SharedContext} ctx - The shared context containing paths and configurations.
|
|
151
|
+
* @param {string} packageName - The name of the package to move.
|
|
152
|
+
* @returns {Promise<void>} A promise that resolves when the package is moved successfully.
|
|
153
|
+
*/ async function movePackage(ctx, packageName) {
|
|
154
|
+
const { vtexProjectPath } = ctx;
|
|
155
|
+
await fs.promises.rename(path.resolve(packageName), path.join(vtexProjectPath, packageName));
|
|
156
|
+
devkit.logger.info(`🚚 Moved package "${packageName}" to VTEX project.`);
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Builds the package, moves it to the VTEX project, and updates the project's package.json.
|
|
160
|
+
*
|
|
161
|
+
* @param {SharedContext} ctx - The shared context containing paths and configurations.
|
|
162
|
+
* @returns {Promise<void>} A promise that resolves when the build is complete.
|
|
163
|
+
*/ async function buildPackage(ctx) {
|
|
164
|
+
const packageName = await createPackage(ctx);
|
|
165
|
+
await Promise.all([
|
|
166
|
+
movePackage(ctx, packageName),
|
|
167
|
+
changePackageJsonFromVtexProject(ctx, packageName)
|
|
168
|
+
]);
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Cleans the VTEX project by removing old packages and restoring the original package.json.
|
|
172
|
+
*
|
|
173
|
+
* @param {SharedContext} ctx - The shared context containing paths and configurations.
|
|
174
|
+
* @returns {Promise<void>} A promise that resolves when the clean-up is complete.
|
|
175
|
+
*/ async function clean(ctx) {
|
|
176
|
+
await Promise.all([
|
|
177
|
+
removeOldPackages(ctx),
|
|
178
|
+
restorePackageJsonFromVtexProject(ctx)
|
|
179
|
+
]);
|
|
180
|
+
}
|
|
181
|
+
function validateEnvironment() {
|
|
182
|
+
if (!constants.VTEX_PROJECT_PATH) throw new Error('The NZ_VTEX_PROJECT environment variable is missing. Set it in the project root in a .env.local file.');
|
|
183
|
+
}
|
|
184
|
+
async function performRollback(sharedContext) {
|
|
185
|
+
await clean(sharedContext);
|
|
186
|
+
return utils.ok();
|
|
187
|
+
}
|
|
188
|
+
async function buildAndLink(sharedContext, skipLink = false) {
|
|
189
|
+
await clean(sharedContext);
|
|
190
|
+
await buildPackage(sharedContext);
|
|
191
|
+
if (!skipLink) await vtexLink(sharedContext);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
exports.buildAndLink = buildAndLink;
|
|
195
|
+
exports.buildPackage = buildPackage;
|
|
196
|
+
exports.changePackageJsonFromVtexProject = changePackageJsonFromVtexProject;
|
|
197
|
+
exports.clean = clean;
|
|
198
|
+
exports.createPackage = createPackage;
|
|
199
|
+
exports.findLastPackages = findLastPackages;
|
|
200
|
+
exports.movePackage = movePackage;
|
|
201
|
+
exports.performRollback = performRollback;
|
|
202
|
+
exports.removeOldPackages = removeOldPackages;
|
|
203
|
+
exports.renamePackage = renamePackage;
|
|
204
|
+
exports.restorePackageJsonFromVtexProject = restorePackageJsonFromVtexProject;
|
|
205
|
+
exports.validateEnvironment = validateEnvironment;
|
|
206
|
+
exports.vtexLink = vtexLink;
|