@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,51 @@
|
|
|
1
|
+
import { logger } from '@nx/devkit';
|
|
2
|
+
import semver from 'semver';
|
|
3
|
+
import '../../shared/logger.mjs';
|
|
4
|
+
import { getPkgJson } from '../../shared/utils.mjs';
|
|
5
|
+
import { getProjectConfig, runCommand, ok, err } from '../shared/utils.mjs';
|
|
6
|
+
import { checkPublishedVersion } from './helpers.mjs';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* This is a minimal script to publish package to "npm".
|
|
10
|
+
* Need to authenticate with NPM before running this script.
|
|
11
|
+
*/ async function runExecutor(opts, ctx) {
|
|
12
|
+
try {
|
|
13
|
+
const { tag, dryRun: isDryRun } = opts;
|
|
14
|
+
const { root: projectRoot } = getProjectConfig(ctx);
|
|
15
|
+
// Read the package.json file and extract the name and version.
|
|
16
|
+
const packageJson = getPkgJson(projectRoot);
|
|
17
|
+
const { name: packageName, version: packageVersion } = packageJson;
|
|
18
|
+
// Check if the package has been previously published and get the published version.
|
|
19
|
+
const { publishedVersion, isFirstVersion } = await checkPublishedVersion(packageName);
|
|
20
|
+
let greaterThanTagVersion, greaterThanLatestVersion;
|
|
21
|
+
if (!isFirstVersion) {
|
|
22
|
+
// Check if the package version is greater than the latest published version for the specified tag.
|
|
23
|
+
greaterThanTagVersion = publishedVersion?.[tag] ? semver.gt(packageVersion, publishedVersion?.[tag]) : true;
|
|
24
|
+
// Check if the package version is greater than the latest published version.
|
|
25
|
+
greaterThanLatestVersion = semver.gt(packageVersion, publishedVersion.latest);
|
|
26
|
+
console.log(`🔎 package.json version: ${packageVersion}`);
|
|
27
|
+
console.log('🔺 published versions:');
|
|
28
|
+
Object.entries(publishedVersion).forEach(([k, v])=>console.log(` * ${k}: ${v}`));
|
|
29
|
+
console.log('\n');
|
|
30
|
+
}
|
|
31
|
+
// Publish the package if it's the first version or if it's a new version.
|
|
32
|
+
const needsPublish = isFirstVersion || greaterThanLatestVersion && greaterThanTagVersion;
|
|
33
|
+
if (needsPublish) {
|
|
34
|
+
const dryRun = isDryRun ? '--dry-run' : '';
|
|
35
|
+
const commandToPublish = `pnpm publish --access public --tag ${tag} --no-git-checks ${dryRun}`;
|
|
36
|
+
isDryRun && logger.info('👀 Skip publish (dry run) \n');
|
|
37
|
+
logger.info(`${commandToPublish} \n`);
|
|
38
|
+
// Execute the command to publish the package.
|
|
39
|
+
// process.chdir(projectRoot);
|
|
40
|
+
await runCommand(commandToPublish, projectRoot);
|
|
41
|
+
logger.info(`\n🚀 Successfully published ${packageName}@${packageVersion}`);
|
|
42
|
+
} else {
|
|
43
|
+
logger.warn(`👻 Package version "${packageVersion}" is invalid.`);
|
|
44
|
+
}
|
|
45
|
+
return ok();
|
|
46
|
+
} catch (error) {
|
|
47
|
+
return err(error, '❌ Error when publishing the package');
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export { runExecutor as default };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "http://json-schema.org/schema",
|
|
3
|
+
"version": 2,
|
|
4
|
+
"title": "Publish executor",
|
|
5
|
+
"description": "",
|
|
6
|
+
"type": "object",
|
|
7
|
+
"properties": {
|
|
8
|
+
"tag": {
|
|
9
|
+
"type": "string",
|
|
10
|
+
"description": "Tag name to publish"
|
|
11
|
+
},
|
|
12
|
+
"dryRun": {
|
|
13
|
+
"type": "boolean",
|
|
14
|
+
"description": "Indicates whether a test execution should be done without really publishing",
|
|
15
|
+
"default": false
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"required": ["tag"]
|
|
19
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { logger } from '@nx/devkit';
|
|
2
|
+
import { exec } from 'child_process';
|
|
3
|
+
import { promisify } from 'util';
|
|
4
|
+
|
|
5
|
+
const execAsync = promisify(exec);
|
|
6
|
+
/**
|
|
7
|
+
* Indicates a successful executor operation.
|
|
8
|
+
*
|
|
9
|
+
* @param {string} message - Optional message to log for the success case.
|
|
10
|
+
* @returns {ExecutorResponse} - An object representing a successful operation.
|
|
11
|
+
*/ function ok(message) {
|
|
12
|
+
message && logger.info(message);
|
|
13
|
+
return {
|
|
14
|
+
success: true
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Indicates a failed executor operation.
|
|
19
|
+
*
|
|
20
|
+
* @param {string | Error | unknown} err - An optional error object or message indicating the nature of the failure.
|
|
21
|
+
* @param {string} message - An optional message to log for the error case. If not provided, a default error message is logged.
|
|
22
|
+
* @returns {ExecutorResponse} - An object representing a failed operation.
|
|
23
|
+
*/ function err(err, message) {
|
|
24
|
+
if (message) {
|
|
25
|
+
logger.error(message);
|
|
26
|
+
} else {
|
|
27
|
+
logger.error(`❌ an error occurred:`);
|
|
28
|
+
}
|
|
29
|
+
if (err) logger.error(err);
|
|
30
|
+
return {
|
|
31
|
+
success: false
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Retrieves the project configuration for the current execution context.
|
|
36
|
+
*
|
|
37
|
+
* @param {ExecutorContext} ctx - The execution context containing project information.
|
|
38
|
+
* @returns {ProjectConfiguration} - The configuration object for the current project.
|
|
39
|
+
* @throws {Error} If the project configuration is invalid or not found.
|
|
40
|
+
*/ function getProjectConfig(ctx) {
|
|
41
|
+
const projectName = ctx.projectName || '';
|
|
42
|
+
const projectConfig = ctx.projectsConfigurations?.projects?.[projectName];
|
|
43
|
+
if (!projectConfig) throw new Error('Invalid project configuration.');
|
|
44
|
+
return projectConfig;
|
|
45
|
+
}
|
|
46
|
+
async function runCommand(command, cwd) {
|
|
47
|
+
try {
|
|
48
|
+
const { stdout, stderr } = await execAsync(command, {
|
|
49
|
+
...cwd && {
|
|
50
|
+
cwd
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
logger.info(stdout);
|
|
54
|
+
logger.info(stderr);
|
|
55
|
+
} catch (error) {
|
|
56
|
+
logger.error(`Unexpected error running command:`);
|
|
57
|
+
logger.error(error);
|
|
58
|
+
throw error;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export { err, getProjectConfig, ok, runCommand };
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { logger } from '@nx/devkit';
|
|
2
|
+
import { promises } from 'fs';
|
|
3
|
+
import { cond, always, T, pipe } from 'ramda';
|
|
4
|
+
|
|
5
|
+
const { writeFile } = promises;
|
|
6
|
+
const transformPathToRoot = (path)=>{
|
|
7
|
+
const distPrefix = './dist/cjs/';
|
|
8
|
+
const srcRegex = /^\.\/src\//;
|
|
9
|
+
const executorTsRegex = /executor\.ts$/;
|
|
10
|
+
const jsonFileRegex = /\.json$/;
|
|
11
|
+
const isJson = jsonFileRegex.test(path);
|
|
12
|
+
const changeSrcPath = (p)=>p.replace(srcRegex, distPrefix);
|
|
13
|
+
const changeFileName = (p)=>isJson ? p : p.replace(executorTsRegex, 'index.cjs');
|
|
14
|
+
return pipe(changeSrcPath, changeFileName)(path);
|
|
15
|
+
};
|
|
16
|
+
const transformPathToDist = (path)=>{
|
|
17
|
+
const cjsPrefix = './cjs/';
|
|
18
|
+
const distRegex = /^\.\/dist\/cjs\//;
|
|
19
|
+
return path.replace(distRegex, cjsPrefix);
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Updates the executor paths for the provided executors object.
|
|
23
|
+
* @param {Executors} executors - The executors object from executors.json.
|
|
24
|
+
* @param {boolean} toDist - Flag to determine if './dist' should be removed from paths.
|
|
25
|
+
* @returns - The updated executors object with transformed paths.
|
|
26
|
+
*/ const updateExecutorPaths = (executors, toDist)=>{
|
|
27
|
+
const transform = cond([
|
|
28
|
+
[
|
|
29
|
+
always(toDist),
|
|
30
|
+
transformPathToDist
|
|
31
|
+
],
|
|
32
|
+
[
|
|
33
|
+
T,
|
|
34
|
+
transformPathToRoot
|
|
35
|
+
]
|
|
36
|
+
]);
|
|
37
|
+
return Object.entries(executors).reduce((updatedExecutors, [key, executor])=>{
|
|
38
|
+
updatedExecutors[key] = {
|
|
39
|
+
...executor,
|
|
40
|
+
implementation: transform(executor.implementation),
|
|
41
|
+
schema: transform(executor.schema)
|
|
42
|
+
};
|
|
43
|
+
return updatedExecutors;
|
|
44
|
+
}, {});
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Writes a JSON file asynchronously.
|
|
48
|
+
* @param {string} path - The file path where to write the JSON data.
|
|
49
|
+
* @param {any} data - The data to be written to the file.
|
|
50
|
+
*/ const writeJsonFileAsync = async (path, data)=>{
|
|
51
|
+
try {
|
|
52
|
+
await writeFile(path, JSON.stringify(data, null, 2));
|
|
53
|
+
logger.info(`✅ Updated: ${path}`);
|
|
54
|
+
} catch (error) {
|
|
55
|
+
logger.error(`❌ Error writing file ${path}: ${error}`);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export { transformPathToDist, transformPathToRoot, updateExecutorPaths, writeJsonFileAsync };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { promises } from 'fs';
|
|
2
|
+
import { join } from 'path';
|
|
3
|
+
import { getProjectConfig, ok, err } from '../shared/utils.mjs';
|
|
4
|
+
import { updateExecutorPaths, writeJsonFileAsync } from './helpers.mjs';
|
|
5
|
+
|
|
6
|
+
const { readFile } = promises;
|
|
7
|
+
/**
|
|
8
|
+
* Executor function to update paths in the executors.json file.
|
|
9
|
+
*
|
|
10
|
+
* @param {ExecutorOptions} _opts - Executor options, currently not used but provided for future extensibility.
|
|
11
|
+
* @param {ExecutorContext} ctx - The executor context, providing access to the project configuration.
|
|
12
|
+
* @returns {Promise<ExecutorResponse>} The result object with a success or error status.
|
|
13
|
+
*/ async function runExecutor(_opts, ctx) {
|
|
14
|
+
try {
|
|
15
|
+
const projectConfig = getProjectConfig(ctx);
|
|
16
|
+
const rootDir = projectConfig.root;
|
|
17
|
+
const executorsFilePath = join(rootDir, 'executors.json');
|
|
18
|
+
const distExecutorsFilePath = join(rootDir, 'dist', 'executors.json');
|
|
19
|
+
const originalContent = JSON.parse(await readFile(executorsFilePath, 'utf8'));
|
|
20
|
+
// Update paths to point to './dist/' and add '.js'
|
|
21
|
+
const updatedExecutors = updateExecutorPaths(originalContent.executors, false);
|
|
22
|
+
// Prepare the version for the dist folder by removing './dist/' from paths
|
|
23
|
+
// const distExecutors = updateExecutorPaths(updatedExecutors, true);
|
|
24
|
+
// Write both files asynchronously and in parallel
|
|
25
|
+
await Promise.all([
|
|
26
|
+
writeJsonFileAsync(executorsFilePath, {
|
|
27
|
+
executors: updatedExecutors
|
|
28
|
+
})
|
|
29
|
+
]);
|
|
30
|
+
return ok(`🚀 Executors updated successfully.`);
|
|
31
|
+
} catch (error) {
|
|
32
|
+
return err(error, `❌ Error processing file: ${error}`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export { runExecutor as default };
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { logger } from '@nx/devkit';
|
|
2
|
+
import { exec, spawn } from 'child_process';
|
|
3
|
+
import { promises, existsSync } from 'fs';
|
|
4
|
+
import { join, resolve } from 'path';
|
|
5
|
+
import { promisify } from 'util';
|
|
6
|
+
import '../../shared/logger.mjs';
|
|
7
|
+
import { getPkgJson, shortId } from '../../shared/utils.mjs';
|
|
8
|
+
import { ok } from '../shared/utils.mjs';
|
|
9
|
+
import { VTEX_PROJECT_PATH, ERROR_LINKING_FLAG, SUCCESS_LINKING_FLAG } from './constants.mjs';
|
|
10
|
+
|
|
11
|
+
const execAsync = promisify(exec);
|
|
12
|
+
const getPkgJsonPath = (vtexProjectPath)=>resolve(vtexProjectPath, 'package.json');
|
|
13
|
+
/**
|
|
14
|
+
* Links the local package to the VTEX environment.
|
|
15
|
+
*
|
|
16
|
+
* @param {SharedContext} ctx - The shared context containing paths and configurations.
|
|
17
|
+
* @returns {Promise<void>} A promise that resolves when the package is linked or rejects on error.
|
|
18
|
+
*/ function vtexLink(ctx) {
|
|
19
|
+
const { vtexProjectPath } = ctx;
|
|
20
|
+
return new Promise((resolve, reject)=>{
|
|
21
|
+
logger.info('🔗 Linking package to VTEX...');
|
|
22
|
+
const link = spawn('vtex', [
|
|
23
|
+
'link',
|
|
24
|
+
'-u'
|
|
25
|
+
], {
|
|
26
|
+
cwd: vtexProjectPath,
|
|
27
|
+
detached: true
|
|
28
|
+
});
|
|
29
|
+
link.stdout.on('data', (chunk)=>{
|
|
30
|
+
const data = chunk.toString('utf8');
|
|
31
|
+
if (data.includes(ERROR_LINKING_FLAG)) {
|
|
32
|
+
reject('could not link package in VTEX');
|
|
33
|
+
}
|
|
34
|
+
if (data.includes(SUCCESS_LINKING_FLAG)) {
|
|
35
|
+
link.pid && process.kill(-link.pid);
|
|
36
|
+
setTimeout(()=>{
|
|
37
|
+
link.kill();
|
|
38
|
+
logger.info('✅ Successfully linked package to VTEX.');
|
|
39
|
+
}, 200);
|
|
40
|
+
resolve();
|
|
41
|
+
}
|
|
42
|
+
logger.info(data);
|
|
43
|
+
});
|
|
44
|
+
link.on('error', (error)=>reject(error.message));
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Updates the package.json file within the VTEX project to use the local version of the package.
|
|
49
|
+
*
|
|
50
|
+
* @param {SharedContext} ctx - The shared context containing paths and configurations.
|
|
51
|
+
* @param {string} packageName - The name of the package to link.
|
|
52
|
+
* @returns {Promise<void>} A promise that resolves when the package.json is updated.
|
|
53
|
+
*/ async function changePackageJsonFromVtexProject(ctx, packageName) {
|
|
54
|
+
const { vtexProjectPath, projectName, packageOrg } = ctx;
|
|
55
|
+
const pkgJsonPath = getPkgJsonPath(vtexProjectPath);
|
|
56
|
+
// Make backup of package.json
|
|
57
|
+
await promises.copyFile(pkgJsonPath, join(vtexProjectPath, 'package.json.nxback'));
|
|
58
|
+
logger.info('🔃 Created package.json backup.');
|
|
59
|
+
// Find and edit value "@${packageOrg}/${projectName}" dependency
|
|
60
|
+
const packageJson = getPkgJson(vtexProjectPath);
|
|
61
|
+
const newPackageJson = {
|
|
62
|
+
...packageJson,
|
|
63
|
+
dependencies: {
|
|
64
|
+
...packageJson.dependencies,
|
|
65
|
+
[`@${packageOrg}/${projectName}`]: `file:./${packageName}`
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
await promises.unlink(pkgJsonPath);
|
|
69
|
+
await promises.writeFile(pkgJsonPath, JSON.stringify(newPackageJson, null, 2));
|
|
70
|
+
logger.info('📝 Updated package.json in VTEX project.');
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Restores the original package.json from the backup created before modifications.
|
|
74
|
+
*
|
|
75
|
+
* @param {SharedContext} ctx - The shared context containing paths and configurations.
|
|
76
|
+
* @returns {Promise<void>} A promise that resolves when the package.json is restored.
|
|
77
|
+
*/ async function restorePackageJsonFromVtexProject(ctx) {
|
|
78
|
+
const { vtexProjectPath } = ctx;
|
|
79
|
+
const pkgJsonPath = getPkgJsonPath(vtexProjectPath);
|
|
80
|
+
const backupFile = resolve(vtexProjectPath, 'package.json.nxback');
|
|
81
|
+
if (!existsSync(backupFile)) return;
|
|
82
|
+
await promises.unlink(pkgJsonPath);
|
|
83
|
+
await promises.copyFile(backupFile, pkgJsonPath);
|
|
84
|
+
await promises.unlink(backupFile);
|
|
85
|
+
logger.info('🔄 Restored package.json in VTEX project.');
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Renames the generated package file with a unique identifier.
|
|
89
|
+
*
|
|
90
|
+
* @param {SharedContext} ctx - The shared context containing paths and configurations.
|
|
91
|
+
* @returns {Promise<string>} A promise that resolves with the new name of the package.
|
|
92
|
+
*/ async function renamePackage(ctx) {
|
|
93
|
+
const { projectRoot, projectName, packageOrg } = ctx;
|
|
94
|
+
const packageJson = getPkgJson('./');
|
|
95
|
+
const oldName = `${packageOrg}-${projectName}-${packageJson.version}.tgz`;
|
|
96
|
+
const oldPath = join(projectRoot, oldName);
|
|
97
|
+
const newName = `${packageOrg}-${projectName}-${packageJson.version}-${shortId(8)}.tgz`;
|
|
98
|
+
const newPath = join(projectRoot, newName);
|
|
99
|
+
await promises.rename(oldPath, newPath);
|
|
100
|
+
logger.info(`🏷️ New package name: ${newName}`);
|
|
101
|
+
return newName;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Creates a pnpm package for uploading to VTEX.
|
|
105
|
+
*
|
|
106
|
+
* @param {SharedContext} ctx - The shared context containing paths and configurations.
|
|
107
|
+
* @returns {Promise<string>} A promise that resolves with the name of the created package.
|
|
108
|
+
*/ async function createPackage(ctx) {
|
|
109
|
+
const command = 'pnpm pack';
|
|
110
|
+
const { stderr } = await execAsync(command);
|
|
111
|
+
if (stderr) logger.error(stderr);
|
|
112
|
+
logger.info('📦 Package generated successfully.');
|
|
113
|
+
return renamePackage(ctx);
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Searches for and lists all outdated packages in the VTEX project directory.
|
|
117
|
+
*
|
|
118
|
+
* @param {SharedContext} ctx - The shared context containing paths and configurations.
|
|
119
|
+
* @returns {Promise<string[]>} A promise that resolves with a list of deprecated package names.
|
|
120
|
+
*/ async function findLastPackages(ctx) {
|
|
121
|
+
const { vtexProjectPath, projectName, packageOrg } = ctx;
|
|
122
|
+
const matchedFiles = [];
|
|
123
|
+
const files = await promises.readdir(vtexProjectPath);
|
|
124
|
+
for (const file of files){
|
|
125
|
+
const regex = new RegExp(`^${packageOrg}-${projectName}-.*$`);
|
|
126
|
+
if (regex.test(file)) {
|
|
127
|
+
matchedFiles.push(file);
|
|
128
|
+
logger.info(`🔍 Found deprecated package: ${file}`);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return matchedFiles;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Removes all outdated packages in the VTEX project directory.
|
|
135
|
+
*
|
|
136
|
+
* @param {SharedContext} ctx - The shared context containing paths and configurations.
|
|
137
|
+
* @returns {Promise<void>} A promise that resolves when the old packages are removed.
|
|
138
|
+
*/ async function removeOldPackages(ctx) {
|
|
139
|
+
const { vtexProjectPath } = ctx;
|
|
140
|
+
const files = await findLastPackages(ctx);
|
|
141
|
+
const toRemove = files.map((x)=>promises.unlink(resolve(vtexProjectPath, x)));
|
|
142
|
+
await Promise.all(toRemove);
|
|
143
|
+
logger.info('🗑️ Removed old packages in VTEX project.');
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Moves a package to the VTEX project directory.
|
|
147
|
+
*
|
|
148
|
+
* @param {SharedContext} ctx - The shared context containing paths and configurations.
|
|
149
|
+
* @param {string} packageName - The name of the package to move.
|
|
150
|
+
* @returns {Promise<void>} A promise that resolves when the package is moved successfully.
|
|
151
|
+
*/ async function movePackage(ctx, packageName) {
|
|
152
|
+
const { vtexProjectPath } = ctx;
|
|
153
|
+
await promises.rename(resolve(packageName), join(vtexProjectPath, packageName));
|
|
154
|
+
logger.info(`🚚 Moved package "${packageName}" to VTEX project.`);
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Builds the package, moves it to the VTEX project, and updates the project's package.json.
|
|
158
|
+
*
|
|
159
|
+
* @param {SharedContext} ctx - The shared context containing paths and configurations.
|
|
160
|
+
* @returns {Promise<void>} A promise that resolves when the build is complete.
|
|
161
|
+
*/ async function buildPackage(ctx) {
|
|
162
|
+
const packageName = await createPackage(ctx);
|
|
163
|
+
await Promise.all([
|
|
164
|
+
movePackage(ctx, packageName),
|
|
165
|
+
changePackageJsonFromVtexProject(ctx, packageName)
|
|
166
|
+
]);
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Cleans the VTEX project by removing old packages and restoring the original package.json.
|
|
170
|
+
*
|
|
171
|
+
* @param {SharedContext} ctx - The shared context containing paths and configurations.
|
|
172
|
+
* @returns {Promise<void>} A promise that resolves when the clean-up is complete.
|
|
173
|
+
*/ async function clean(ctx) {
|
|
174
|
+
await Promise.all([
|
|
175
|
+
removeOldPackages(ctx),
|
|
176
|
+
restorePackageJsonFromVtexProject(ctx)
|
|
177
|
+
]);
|
|
178
|
+
}
|
|
179
|
+
function validateEnvironment() {
|
|
180
|
+
if (!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.');
|
|
181
|
+
}
|
|
182
|
+
async function performRollback(sharedContext) {
|
|
183
|
+
await clean(sharedContext);
|
|
184
|
+
return ok();
|
|
185
|
+
}
|
|
186
|
+
async function buildAndLink(sharedContext, skipLink = false) {
|
|
187
|
+
await clean(sharedContext);
|
|
188
|
+
await buildPackage(sharedContext);
|
|
189
|
+
if (!skipLink) await vtexLink(sharedContext);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export { buildAndLink, buildPackage, changePackageJsonFromVtexProject, clean, createPackage, findLastPackages, movePackage, performRollback, removeOldPackages, renamePackage, restorePackageJsonFromVtexProject, validateEnvironment, vtexLink };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { resolve } from 'path';
|
|
2
|
+
import { getProjectConfig, ok, err } from '../shared/utils.mjs';
|
|
3
|
+
import { validateEnvironment, performRollback, buildAndLink } from './helpers.mjs';
|
|
4
|
+
|
|
5
|
+
const VTEX_PROJECT_PATH = process.env.NZ_VTEX_PROJECT;
|
|
6
|
+
async function runExecutor(opts, ctx) {
|
|
7
|
+
try {
|
|
8
|
+
const { rollback, skipLink, packageOrg } = opts;
|
|
9
|
+
const projectName = ctx.projectName || '';
|
|
10
|
+
validateEnvironment();
|
|
11
|
+
const projectConfig = getProjectConfig(ctx);
|
|
12
|
+
const projectRoot = projectConfig.root;
|
|
13
|
+
const vtexProjectPath = resolve(VTEX_PROJECT_PATH, 'react');
|
|
14
|
+
const sharedContext = {
|
|
15
|
+
packageOrg,
|
|
16
|
+
projectRoot,
|
|
17
|
+
projectName,
|
|
18
|
+
vtexProjectPath
|
|
19
|
+
};
|
|
20
|
+
if (rollback) return await performRollback(sharedContext);
|
|
21
|
+
await buildAndLink(sharedContext, skipLink);
|
|
22
|
+
return ok();
|
|
23
|
+
} catch (error) {
|
|
24
|
+
return err(error);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export { runExecutor as default };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "http://json-schema.org/schema",
|
|
3
|
+
"version": 2,
|
|
4
|
+
"title": "VtexLink executor",
|
|
5
|
+
"description": "",
|
|
6
|
+
"type": "object",
|
|
7
|
+
"properties": {
|
|
8
|
+
"packageOrg": {
|
|
9
|
+
"type": "string",
|
|
10
|
+
"description": "Name of the npm organization in which the packages are published"
|
|
11
|
+
},
|
|
12
|
+
"rollback": {
|
|
13
|
+
"type": "boolean",
|
|
14
|
+
"description": "Perform a rollback.",
|
|
15
|
+
"default": false
|
|
16
|
+
},
|
|
17
|
+
"skipLink": {
|
|
18
|
+
"type": "boolean",
|
|
19
|
+
"description": "Skip the linking step.",
|
|
20
|
+
"default": false
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"required": [],
|
|
24
|
+
"additionalProperties": true
|
|
25
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export { default as deployExecutor } from './executors/deploy/index.mjs';
|
|
2
|
+
export { default as environmentsExecutor } from './executors/environments/index.mjs';
|
|
3
|
+
export { default as publishExecutor } from './executors/publish/index.mjs';
|
|
4
|
+
export { default as updateExecutorsFileExecutor } from './executors/update-executors-file/index.mjs';
|
|
5
|
+
export { default as vtexLinkExecutor } from './executors/vtex-link/index.mjs';
|
|
6
|
+
export { createConfigBuilder } from './vite-utils/config-builder/main.mjs';
|
|
7
|
+
export { Flag, ProjectType, Stage, Target, ViteMode } from './vite-utils/config-builder/shared/types.mjs';
|
|
8
|
+
export { chunkName, deepMerge, getNxTsConfigPaths, getPlugins, when } from './vite-utils/config-builder/shared/utils/helpers.mjs';
|
|
9
|
+
export { getExternalDependencies, getInitials, initializeConfig, mergeConfigs } from './vite-utils/config-builder/shared/utils/projectConfig.mjs';
|
|
10
|
+
export { getViteCmdFlags, getViteCommand } from './vite-utils/config-builder/shared/utils/viteCommand.mjs';
|
|
11
|
+
export { thirdPartyCssInjectPlugin } from './vite-utils/plugins/third-party-css-inject/main.mjs';
|
|
12
|
+
export { injectCssIntoShadowDom } from './vite-utils/plugins/shadow-dom-inject-css/index.mjs';
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { logger } from '@nx/devkit';
|
|
2
|
+
import { exec } from 'child_process';
|
|
3
|
+
import { readFile } from 'fs/promises';
|
|
4
|
+
import { resolve } from 'path';
|
|
5
|
+
import { promisify } from 'util';
|
|
6
|
+
|
|
7
|
+
const execAsync = promisify(exec);
|
|
8
|
+
const changesetDir = resolve('.changeset');
|
|
9
|
+
const preJsonPath = resolve(changesetDir, 'pre.json');
|
|
10
|
+
let preJson;
|
|
11
|
+
async function main() {
|
|
12
|
+
try {
|
|
13
|
+
const data = await readFile(preJsonPath, 'utf8');
|
|
14
|
+
preJson = JSON.parse(data);
|
|
15
|
+
} catch {
|
|
16
|
+
preJson = {
|
|
17
|
+
mode: 'exit'
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
// Execute "version" command using @changeset/cli
|
|
21
|
+
try {
|
|
22
|
+
const { mode } = preJson;
|
|
23
|
+
if (mode === 'exit') {
|
|
24
|
+
const command = `pnpm changeset pre enter beta`;
|
|
25
|
+
const { stdout, stderr } = await execAsync(command);
|
|
26
|
+
logger.info(stdout);
|
|
27
|
+
logger.info(stderr);
|
|
28
|
+
logger.info(`🚀 Successfully enter prerelease mode`);
|
|
29
|
+
} else {
|
|
30
|
+
logger.info(`🚀 Prerelease mode has been previously entered`);
|
|
31
|
+
}
|
|
32
|
+
} catch (error) {
|
|
33
|
+
logger.error('❌ An error occurred!');
|
|
34
|
+
throw error;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
main();
|
|
38
|
+
|
|
39
|
+
export { main };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { main } from './changeset-pre-mode.mjs';
|
|
@@ -1,33 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
});
|
|
5
|
-
function _export(target, all) {
|
|
6
|
-
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
-
enumerable: true,
|
|
8
|
-
get: all[name]
|
|
9
|
-
});
|
|
10
|
-
}
|
|
11
|
-
_export(exports, {
|
|
12
|
-
LogLevel: function() {
|
|
13
|
-
return LogLevel;
|
|
14
|
-
},
|
|
15
|
-
currentLogLevel: function() {
|
|
16
|
-
return currentLogLevel;
|
|
17
|
-
},
|
|
18
|
-
default: function() {
|
|
19
|
-
return _default;
|
|
20
|
-
},
|
|
21
|
-
logger: function() {
|
|
22
|
-
return logger;
|
|
23
|
-
},
|
|
24
|
-
setLogLevel: function() {
|
|
25
|
-
return setLogLevel;
|
|
26
|
-
}
|
|
27
|
-
});
|
|
28
|
-
const _interop_require_default = require("@swc/helpers/_/_interop_require_default");
|
|
29
|
-
const _chalk = /*#__PURE__*/ _interop_require_default._(require("chalk"));
|
|
30
|
-
const _ramda = require("ramda");
|
|
1
|
+
import chalk from 'chalk';
|
|
2
|
+
import { pipe, map, join } from 'ramda';
|
|
3
|
+
|
|
31
4
|
var LogLevel;
|
|
32
5
|
(function(LogLevel) {
|
|
33
6
|
LogLevel[LogLevel["ERROR"] = 0] = "ERROR";
|
|
@@ -35,6 +8,7 @@ var LogLevel;
|
|
|
35
8
|
LogLevel[LogLevel["INFO"] = 2] = "INFO";
|
|
36
9
|
LogLevel[LogLevel["DEBUG"] = 3] = "DEBUG";
|
|
37
10
|
})(LogLevel || (LogLevel = {}));
|
|
11
|
+
// Default log level
|
|
38
12
|
let currentLogLevel = 2;
|
|
39
13
|
/**
|
|
40
14
|
* Object mapping log levels to console log functions.
|
|
@@ -51,15 +25,29 @@ let currentLogLevel = 2;
|
|
|
51
25
|
*
|
|
52
26
|
* @constant {Object} colorFunctions
|
|
53
27
|
*/ const colorFunctions = {
|
|
54
|
-
[0]:
|
|
55
|
-
[1]:
|
|
56
|
-
[2]:
|
|
57
|
-
[3]:
|
|
28
|
+
[0]: chalk.red,
|
|
29
|
+
[1]: chalk.yellow,
|
|
30
|
+
[2]: chalk.blue,
|
|
31
|
+
[3]: chalk.magenta
|
|
58
32
|
};
|
|
59
33
|
const getLogLevelKey = (value)=>{
|
|
60
34
|
return Object.keys(LogLevel).find((key)=>LogLevel[key] === value);
|
|
61
35
|
};
|
|
62
|
-
|
|
36
|
+
/**
|
|
37
|
+
* Sets the log level for the logger.
|
|
38
|
+
* Only logs at this level and above will be displayed.
|
|
39
|
+
*
|
|
40
|
+
* @param {LogLevel} level - The log level to set.
|
|
41
|
+
* @example
|
|
42
|
+
* // Set log level to WARN
|
|
43
|
+
* setLogLevel(LogLevel.WARN);
|
|
44
|
+
*
|
|
45
|
+
* // Now only WARN and ERROR logs will be displayed
|
|
46
|
+
* logger.debug('Debug message'); // Will not be logged
|
|
47
|
+
* logger.info('Info message'); // Will not be logged
|
|
48
|
+
* logger.warn('Warn message'); // Will be logged
|
|
49
|
+
* logger.error('Error message'); // Will be logged
|
|
50
|
+
*/ const setLogLevel = (level)=>{
|
|
63
51
|
currentLogLevel = level;
|
|
64
52
|
};
|
|
65
53
|
/**
|
|
@@ -68,7 +56,7 @@ const setLogLevel = (level)=>{
|
|
|
68
56
|
*
|
|
69
57
|
* @param {any[]} args - The arguments to process.
|
|
70
58
|
* @returns {string} - The processed string.
|
|
71
|
-
*/ const processArgs =
|
|
59
|
+
*/ const processArgs = pipe(map((arg)=>JSON.stringify(arg, null, 2)), join('\n'));
|
|
72
60
|
/**
|
|
73
61
|
* Formats the log message with a timestamp, log level, and any additional variables.
|
|
74
62
|
*
|
|
@@ -89,8 +77,8 @@ const setLogLevel = (level)=>{
|
|
|
89
77
|
* @returns - The log function.
|
|
90
78
|
*/ const log = (level)=>(message, ...rest)=>{
|
|
91
79
|
if (level > currentLogLevel) return; // Skip logging if level is below the current log level
|
|
92
|
-
const pipeline =
|
|
93
|
-
const errPipeline =
|
|
80
|
+
const pipeline = pipe(()=>formatMessage(level, message, ...rest), colorFunctions[level], logFunctions[level]);
|
|
81
|
+
const errPipeline = pipe((err)=>`Failed to log message: ${err.message}`, colorFunctions[0], logFunctions[0]);
|
|
94
82
|
try {
|
|
95
83
|
pipeline();
|
|
96
84
|
} catch (err) {
|
|
@@ -103,4 +91,5 @@ const logger = {
|
|
|
103
91
|
info: log(2),
|
|
104
92
|
debug: log(3)
|
|
105
93
|
};
|
|
106
|
-
|
|
94
|
+
|
|
95
|
+
export { LogLevel, currentLogLevel, logger, setLogLevel };
|