Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DX - 77: Added the missing modules handle function, upgraded uuid and version bump #1455

Merged
merged 5 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/contentstack-import/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"mkdirp": "^1.0.4",
"promise-limit": "^2.7.0",
"tslib": "^2.4.1",
"uuid": "^9.0.0",
"uuid": "^9.0.1",
"winston": "^3.7.2"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions packages/contentstack-migrate-rte/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"nock": "^13.1.0",
"omit-deep-lodash": "^1.1.5",
"sinon": "^15.0.1",
"uuid": "^9.0.0"
"uuid": "^9.0.1"
},
"devDependencies": {
"@oclif/test": "^2.5.6",
Expand Down Expand Up @@ -67,4 +67,4 @@
"cm:migrate-rte": "O-MGRTRTE"
}
}
}
}
2 changes: 1 addition & 1 deletion packages/contentstack-migration/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ $ npm install -g @contentstack/cli-migration
$ csdx COMMAND
running command...
$ csdx (--version)
@contentstack/cli-migration/1.5.6 darwin-arm64 node-v22.2.0
@contentstack/cli-migration/1.6.0 darwin-arm64 node-v22.2.0
$ csdx --help [COMMAND]
USAGE
$ csdx COMMAND
Expand Down
4 changes: 2 additions & 2 deletions packages/contentstack-migration/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@contentstack/cli-migration",
"version": "1.5.6",
"version": "1.6.0",
"author": "@contentstack",
"bugs": "https://github.com/contentstack/cli/issues",
"dependencies": {
Expand Down Expand Up @@ -66,4 +66,4 @@
"cm:migration": "O-MGRTN"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const {
const { ApiError, SchemaValidator, MigrationError, FieldValidator } = require('../../../validators');

// Utils
const { map: _map, constants, safePromise, errorHelper } = require('../../../utils');
const { map: _map, constants, safePromise, errorHelper, installModules } = require('../../../utils');
// Properties
const { get, set, getMapInstance, resetMapInstance } = _map;
const {
Expand Down Expand Up @@ -67,7 +67,7 @@ class MigrationCommand extends Command {
this.exit();
}

if (!filePath) {
if (!filePath || !fs.existsSync(filePath)) {
this.log('Please provide the migration script file path, use --file-path flag');
this.exit();
}
Expand Down Expand Up @@ -129,6 +129,11 @@ class MigrationCommand extends Command {
set(MANAGEMENT_SDK, mapInstance, stackSDKInstance);
set(MANAGEMENT_CLIENT, mapInstance, APIClient);

if (!(await installModules(filePath, multi))) {
this.log(`Failed to install the dependencies of the given scripts.`);
process.exit(1);
}

if (multi) {
await this.execMultiFiles(filePath, mapInstance);
} else {
Expand Down
1 change: 1 addition & 0 deletions packages/contentstack-migration/src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ module.exports = {
getBatches: require('./get-batches'),
autoRetry: require('./auto-retry'),
contentstackSdk: require('./contentstack-sdk'),
installModules: require('./modules'),
};
134 changes: 134 additions & 0 deletions packages/contentstack-migration/src/utils/modules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
const fs = require('fs');
const { execSync } = require('child_process');
const path = require('path');
const { sanitizePath } = require('@contentstack/cli-utilities');
const os = require('os');
const { builtinModules } = require('module');

const internalModules = new Set(builtinModules);

function checkWritePermissionToDirectory(directory) {
try {
fs.accessSync(directory, fs.constants.W_OK);
return true;
} catch (err) {
console.log(`You don't have write access to directory`);
return false;
}
}

function doesPackageJsonExist(directory) {
return fs.existsSync(path.join(sanitizePath(directory), 'package.json'));
}

function scanDirectory(directory) {
return fs.readdirSync(directory);
}

function scanFileForDependencies(directory, files) {
const dependencies = new Set();

files.forEach((file) => {
const filePath = path.join(sanitizePath(directory), sanitizePath(file));
if (path.extname(filePath) === '.js') {
const fileContent = fs.readFileSync(filePath, 'utf-8');
findModulesSync(fileContent).forEach((dep) => dependencies.add(dep));
}
});

return [...dependencies];
}

function createPackageJson(directory) {
const templateString = `{
"name": "MigrationPackage",
"version": "1.0.0",
"main": "",
"scripts": {},
"keywords": [],
"author": "",
"license": "ISC",
"description": ""
}`;

fs.writeFileSync(path.join(sanitizePath(directory), 'package.json'), templateString);
}

function installDependencies(dependencies, directory) {
cs-raj marked this conversation as resolved.
Show resolved Hide resolved
const installedDependencies = new Set();

dependencies.forEach((dep) => {
if (!internalModules.has(dep)) {
const pkg = dep.startsWith('@') ? dep : dep.split('/')[0];
if (!installedDependencies.has(pkg)) {
executeShellCommand(`npm i ${pkg}`, directory);
installedDependencies.add(pkg);
}
}
});
}

function executeShellCommand(command, directory = '') {
try {
execSync(command, { stdio: 'inherit', cwd: directory });
console.log(`Command executed successfully: ${command}`);
} catch (error) {
console.error(`Error: ${error.message}`);
}
}

async function installModules(filePath, multiple) {
const files = multiple ? [] : [path.basename(filePath)];
const dirPath = multiple ? filePath : path.dirname(filePath);

if (checkWritePermissionToDirectory(dirPath)) {
if (multiple) {
files.push(...scanDirectory(dirPath));
}

if (files.length === 0) {
console.log(`There are no files to create package.json for, exiting the code`);
return true;
}

const dependencies = scanFileForDependencies(dirPath, files);

if (!doesPackageJsonExist(dirPath)) {
console.log(`package.json doesn't exist, creating one`);
createPackageJson(dirPath);
}

installDependencies(dependencies, dirPath);
} else {
console.log(`You don't have write permission to the directory`);
return false;
}

console.log(`All dependencies installed successfully`);
return true;
}

function findModulesSync(data) {
try {
const requireRegex = /require\(['"`](.*?)['"`]\)/g;
const importRegex = /import\s+(?:(?:[\w*\s{},]*)\s+from\s+)?['"`](.*?)['"`]/g;

const modules = new Set();
let match;

while ((match = requireRegex.exec(data)) !== null) {
modules.add(match[1]);
}

while ((match = importRegex.exec(data)) !== null) {
modules.add(match[1]);
}

return [...modules];
} catch (error) {
console.error(`Error reading file: ${error.message}`);
return [];
}
}

module.exports = installModules;
2 changes: 1 addition & 1 deletion packages/contentstack-utilities/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"rxjs": "^6.6.7",
"traverse": "^0.6.7",
"unique-string": "^2.0.0",
"uuid": "^9.0.0",
"uuid": "^9.0.1",
"winston": "^3.7.2",
"xdg-basedir": "^4.0.0"
},
Expand Down
16 changes: 8 additions & 8 deletions packages/contentstack/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ $ npm install -g @contentstack/cli
$ csdx COMMAND
running command...
$ csdx (--version|-v)
@contentstack/cli/1.20.0 darwin-arm64 node-v22.2.0
@contentstack/cli/1.21.0 darwin-arm64 node-v22.2.0
$ csdx --help [COMMAND]
USAGE
$ csdx COMMAND
Expand Down Expand Up @@ -3496,7 +3496,7 @@ EXAMPLES
$ csdx plugins
```

_See code: [@oclif/plugin-plugins](https://github.com/oclif/plugin-plugins/blob/v5.3.3/src/commands/plugins/index.ts)_
_See code: [@oclif/plugin-plugins](https://github.com/oclif/plugin-plugins/blob/v5.2.4/src/commands/plugins/index.ts)_

## `csdx plugins:add PLUGIN`

Expand Down Expand Up @@ -3570,7 +3570,7 @@ EXAMPLES
$ csdx plugins:inspect myplugin
```

_See code: [@oclif/plugin-plugins](https://github.com/oclif/plugin-plugins/blob/v5.3.3/src/commands/plugins/inspect.ts)_
_See code: [@oclif/plugin-plugins](https://github.com/oclif/plugin-plugins/blob/v5.2.4/src/commands/plugins/inspect.ts)_

## `csdx plugins:install PLUGIN`

Expand Down Expand Up @@ -3619,7 +3619,7 @@ EXAMPLES
$ csdx plugins:install someuser/someplugin
```

_See code: [@oclif/plugin-plugins](https://github.com/oclif/plugin-plugins/blob/v5.3.3/src/commands/plugins/install.ts)_
_See code: [@oclif/plugin-plugins](https://github.com/oclif/plugin-plugins/blob/v5.2.4/src/commands/plugins/install.ts)_

## `csdx plugins:link PATH`

Expand Down Expand Up @@ -3649,7 +3649,7 @@ EXAMPLES
$ csdx plugins:link myplugin
```

_See code: [@oclif/plugin-plugins](https://github.com/oclif/plugin-plugins/blob/v5.3.3/src/commands/plugins/link.ts)_
_See code: [@oclif/plugin-plugins](https://github.com/oclif/plugin-plugins/blob/v5.2.4/src/commands/plugins/link.ts)_

## `csdx plugins:remove [PLUGIN]`

Expand Down Expand Up @@ -3690,7 +3690,7 @@ FLAGS
--reinstall Reinstall all plugins after uninstalling.
```

_See code: [@oclif/plugin-plugins](https://github.com/oclif/plugin-plugins/blob/v5.3.3/src/commands/plugins/reset.ts)_
_See code: [@oclif/plugin-plugins](https://github.com/oclif/plugin-plugins/blob/v5.2.4/src/commands/plugins/reset.ts)_

## `csdx plugins:uninstall [PLUGIN]`

Expand Down Expand Up @@ -3718,7 +3718,7 @@ EXAMPLES
$ csdx plugins:uninstall myplugin
```

_See code: [@oclif/plugin-plugins](https://github.com/oclif/plugin-plugins/blob/v5.3.3/src/commands/plugins/uninstall.ts)_
_See code: [@oclif/plugin-plugins](https://github.com/oclif/plugin-plugins/blob/v5.2.4/src/commands/plugins/uninstall.ts)_

## `csdx plugins:unlink [PLUGIN]`

Expand Down Expand Up @@ -3762,7 +3762,7 @@ DESCRIPTION
Update installed plugins.
```

_See code: [@oclif/plugin-plugins](https://github.com/oclif/plugin-plugins/blob/v5.3.3/src/commands/plugins/update.ts)_
_See code: [@oclif/plugin-plugins](https://github.com/oclif/plugin-plugins/blob/v5.2.4/src/commands/plugins/update.ts)_

## `csdx tokens`

Expand Down
6 changes: 3 additions & 3 deletions packages/contentstack/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@contentstack/cli",
"description": "Command-line tool (CLI) to interact with Contentstack",
"version": "1.20.0",
"version": "1.21.0",
"author": "Contentstack",
"bin": {
"csdx": "./bin/run.js"
Expand Down Expand Up @@ -36,7 +36,7 @@
"@contentstack/cli-command": "~1.2.18",
"@contentstack/cli-config": "~1.6.4",
"@contentstack/cli-launch": "~1.0.19",
"@contentstack/cli-migration": "~1.5.6",
"@contentstack/cli-migration": "~1.6.0",
"@contentstack/cli-utilities": "~1.6.3",
"@contentstack/management": "~1.15.3",
"@oclif/core": "^3.26.5",
Expand All @@ -50,7 +50,7 @@
"node-machine-id": "^1.1.12",
"open": "^8.4.2",
"short-uuid": "^4.2.2",
"uuid": "^9.0.0",
"uuid": "^9.0.1",
"winston": "^3.7.2"
},
"devDependencies": {
Expand Down
Loading
Loading