@cocreate/cli 1.49.0 → 1.51.0

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/src/addMeta.js CHANGED
@@ -1,75 +1,82 @@
1
- const fs = require('fs')
2
- const path = require("path")
3
- const util = require('node:util');
4
- const exec = util.promisify(require('node:child_process').exec);
1
+ const fs = require("fs");
2
+ const path = require("path");
3
+ const util = require("node:util");
4
+ const exec = util.promisify(require("node:child_process").exec);
5
5
 
6
6
  module.exports = async function addMeta(repos, failed, directory) {
7
- let packageManager;
8
- for (let i = 0; i < repos.length; i++) {
9
- repos[i].name = path.basename(repos[i].path);
10
- repos[i].plainName = repos[i].name.substring(9);
7
+ let packageManager;
8
+ for (let i = 0; i < repos.length; i++) {
9
+ repos[i].name = path.basename(repos[i].path);
10
+ repos[i].plainName = repos[i].name.substring(9);
11
11
 
12
- if (directory) {
13
- repos[i].absolutePath = path.resolve(directory, repos[i].path);
14
- const parsedPath = path.parse(repos[i].absolutePath);
15
- repos[i].directory = parsedPath.dir
16
- }
12
+ if (directory) {
13
+ repos[i].absolutePath = path.resolve(directory, repos[i].path);
14
+ const parsedPath = path.parse(repos[i].absolutePath);
15
+ repos[i].directory = parsedPath.dir;
16
+ }
17
17
 
18
- let packagejson = path.resolve(repos[i].absolutePath, 'package.json');
19
- if (!fs.existsSync(packagejson)) {
20
- console.error('package json not found for', repos[i].name);
21
- failed.push({
22
- name: repos[i].name,
23
- des: 'package json not found'
24
- })
25
- } else {
18
+ let packagejson = path.resolve(repos[i].absolutePath, "package.json");
19
+ if (!fs.existsSync(packagejson)) {
20
+ console.error("package json not found for", repos[i].name);
21
+ failed.push({
22
+ name: repos[i].name,
23
+ error: "package json not found"
24
+ });
25
+ } else {
26
+ let packageObj;
27
+ try {
28
+ packageObj = require(packagejson);
29
+ } catch (err) {
30
+ console.error("packageObj", err.message);
31
+ }
26
32
 
27
- let packageObj
28
- try {
29
- packageObj = require(packagejson);
30
- }
31
- catch (err) {
32
- console.error('packageObj', err.message)
33
- }
33
+ repos[i].packageName = packageObj.name;
34
34
 
35
+ repos[i].deps = Object.keys(
36
+ packageObj["dependencies"] || {}
37
+ ).filter((packageName) => packageName.startsWith("@cocreate/"));
38
+ repos[i].devDeps = Object.keys(
39
+ packageObj["devDependencies"] || {}
40
+ ).filter((packageName) => packageName.startsWith("@cocreate/"));
35
41
 
36
- repos[i].packageName = packageObj.name;
42
+ if (!repos[i].packageManager) {
43
+ if (packageManager) repos[i].packageManager = packageManager;
44
+ else {
45
+ repos[i].packageManager = "npm";
46
+ let lockFile = path.resolve(
47
+ repos[i].absolutePath,
48
+ "package-lock.json"
49
+ );
50
+ if (!fs.existsSync(lockFile)) {
51
+ lockFile = path.resolve(
52
+ repos[i].absolutePath,
53
+ "pnpm-lock.yaml"
54
+ );
55
+ if (fs.existsSync(lockFile))
56
+ repos[i].packageManager = "pnpm";
57
+ else {
58
+ lockFile = path.resolve(
59
+ repos[i].absolutePath,
60
+ "yarn.lock"
61
+ );
62
+ if (fs.existsSync(lockFile))
63
+ repos[i].packageManager = "yarn";
64
+ else {
65
+ try {
66
+ const { error } = await exec(
67
+ "yarn --version"
68
+ );
69
+ if (!error)
70
+ repos[i].packageManager = "yarn";
71
+ } catch (e) {}
72
+ }
73
+ }
74
+ packageManager = repos[i].packageManager;
75
+ }
76
+ }
77
+ }
78
+ }
79
+ }
37
80
 
38
- repos[i].deps = Object.keys(packageObj['dependencies'] || {})
39
- .filter(packageName => packageName.startsWith('@cocreate/'));
40
- repos[i].devDeps = Object.keys(packageObj['devDependencies'] || {})
41
- .filter(packageName => packageName.startsWith('@cocreate/'));
42
-
43
- if (!repos[i].packageManager) {
44
- if (packageManager)
45
- repos[i].packageManager = packageManager
46
- else {
47
- repos[i].packageManager = 'npm'
48
- let lockFile = path.resolve(repos[i].absolutePath, 'package-lock.json');
49
- if (!fs.existsSync(lockFile)) {
50
- lockFile = path.resolve(repos[i].absolutePath, 'pnpm-lock.yaml');
51
- if (fs.existsSync(lockFile))
52
- repos[i].packageManager = 'pnpm'
53
- else {
54
- lockFile = path.resolve(repos[i].absolutePath, 'yarn.lock');
55
- if (fs.existsSync(lockFile))
56
- repos[i].packageManager = 'yarn'
57
- else {
58
- try {
59
- const { error } = await exec('yarn --version');
60
- if (!error)
61
- repos[i].packageManager = 'yarn'
62
- } catch(e) {
63
-
64
- }
65
- }
66
- }
67
- packageManager = repos[i].packageManager
68
- }
69
- }
70
- }
71
- }
72
- }
73
-
74
- return repos
75
- }
81
+ return repos;
82
+ };
package/src/coc.js CHANGED
@@ -1,115 +1,167 @@
1
1
  #!/usr/bin/env node
2
- const path = require("path");
3
- const fs = require("fs");
4
- const execute = require('./execute');
5
- const argv = process.argv.slice(2);
6
- const addMeta = require('./addMeta');
7
- const { color } = require('./fonts');
8
-
9
-
10
- if (argv.length < 1) {
11
- console.error("enter some command to do something");
12
- process.exit(1);
13
- }
14
- let repos, command, config = {};
15
-
16
- const options = ['-self']
17
- for (let option of options) {
18
- if (argv.includes(option)) {
19
- config.self = true
20
- const index = argv.indexOf(option);
21
- delete argv[index];
22
- }
23
- }
24
-
25
- command = argv
26
- .map((part) => part.match(/ |'|"/) ? `'${part.replace(/'/, '\\\'')}'` : part)
27
- .join(" ");
28
-
29
- function getRepositories(path) {
30
- try {
31
- const config = require(path);
32
- return config.repositories;
33
- }
34
- catch (err) {
35
- console.error(color.red + 'can not read repository file in' + color.reset, path, color.red + 'error:' + color.reset, err.message);
36
- process.exit(1);
37
- }
38
- }
39
-
40
- // TODO: handle getting closest config
41
- async function getConfig(directory, filename = '') {
42
- const filePath = path.resolve(directory, filename);
43
- if (!filePath.includes('node_modules')) {
44
- const configPath = findClosestConfig(filePath)
45
- if (configPath) {
46
- return { config: require(configPath), configPath, filePath };
47
-
48
- } else {
49
- console.log('No CoCreate.config file found in parent directories.');
50
- }
51
- }
52
-
53
- }
54
-
55
- function findClosestConfig(filePath) {
56
- let currentDir = filePath;
57
-
58
- while (currentDir !== '/' && currentDir !== '.') {
59
- let configFile = path.join(currentDir, 'CoCreate.config.js');
60
-
61
- if (fs.existsSync(configFile)) {
62
- return configFile;
63
- }
64
-
65
- currentDir = path.dirname(currentDir);
66
- }
67
-
68
- return null;
69
- }
70
-
71
-
72
- const currentRepoPath = path.resolve(process.cwd(), "CoCreate.config.js");
73
- let packageJsonPath = path.resolve(process.cwd(), 'package.json');
74
- let directory
75
-
76
- if (config['c'] && fs.existsSync(config['c'])) {
77
- repos = getRepositories(config['c']);
78
- directory = path.dirname(config['c']);
79
- console.warn(color.yellow + `using ${config['c']} configuration` + color.reset);
80
- } else if (!config['self'] && fs.existsSync(currentRepoPath)) {
81
- repos = getRepositories(currentRepoPath);
82
- directory = path.dirname(currentRepoPath);
83
- console.warn(color.yellow + `using ${currentRepoPath} configuration` + color.reset);
84
- } else if (fs.existsSync(packageJsonPath)) {
85
- let repoPath = path.resolve(process.cwd());
86
- let packageObj = require(packageJsonPath);
87
- let repoUrl = packageObj.repository.url.substring(12);
88
- repos = [{
89
- path: `${repoPath}`,
90
- repo: `${repoUrl}`
91
- }];
92
- directory = path.dirname(packageJsonPath);
93
- console.warn(color.yellow + `using ${packageJsonPath} configuration` + color.reset);
94
- }
95
- // else {
96
- // console.error(color.red + `a configuration file can not be found` + color.reset);
97
- // process.exit(1);
98
- // }
99
- config = { hideMessage: false, ...config };
100
-
101
- (async () => {
102
- if (repos && repos.length)
103
- repos = await addMeta(repos, [], directory)
104
- let failed = await execute(command, repos, config);
105
- if (failed) {
106
- if (failed.length === 0)
107
- process.exit(0);
108
- else {
109
- console.log(color.red + ' **************** failures **************** ' + color.reset);
110
- for (let failure of failed)
111
- console.log(color.red + `${failure.name}: ${failure.des}` + color.reset);
112
-
113
- }
114
- }
115
- })();
2
+
3
+ // Import necessary modules for path operations, file system operations, and more
4
+ const path = require("path");
5
+ const fs = require("fs");
6
+ const readline = require("readline");
7
+ const execute = require("./execute");
8
+ const addMeta = require("./addMeta");
9
+ const { color } = require("./fonts");
10
+
11
+ // Configuration object for storing options
12
+ let config = {};
13
+
14
+ // Extract arguments from the command line input
15
+ const argv = process.argv.slice(2);
16
+
17
+ // Define available command-line options
18
+ const options = ["-self"];
19
+
20
+ // Iterate over available options and set configurations if specified in argv
21
+ for (let option of options) {
22
+ if (argv.includes(option)) {
23
+ config[option.replace(/^--/, "")] = true;
24
+ const index = argv.indexOf(option);
25
+ delete argv[index];
26
+ }
27
+ }
28
+
29
+ // Format command from argv, handling spaces and quotes
30
+ command = argv
31
+ .map((part) => (part.match(/ |'|"/) ? `'${part.replace(/'/, "'")}'` : part))
32
+ .join(" ");
33
+
34
+ /**
35
+ * Load repository configuration from the given path.
36
+ * @param {string} path - The file path to load repository config from.
37
+ * @returns {Array} - List of repositories.
38
+ */
39
+ function getRepositories(path) {
40
+ try {
41
+ const config = require(path);
42
+ return config.repositories;
43
+ } catch (err) {
44
+ console.error(
45
+ color.red + "cannot read repository file in" + color.reset,
46
+ path,
47
+ color.red + "error:" + color.reset,
48
+ err.message
49
+ );
50
+ process.exit(1);
51
+ }
52
+ }
53
+
54
+ /**
55
+ * Main function to execute commands across repositories.
56
+ * @param {Object} config - The configuration object.
57
+ * @param {Array} [repos=null] - List of repositories to process.
58
+ * @param {string} [directory=null] - The directory path of the configuration.
59
+ */
60
+ async function main(config = {}, repos = null, directory = null) {
61
+ if (!repos) {
62
+ // Determine repositories and configuration file paths
63
+ const currentRepoPath = path.resolve(
64
+ process.cwd(),
65
+ "CoCreate.config.js"
66
+ );
67
+ const packageJsonPath = path.resolve(process.cwd(), "package.json");
68
+
69
+ // Load repositories from specified config file
70
+ if (config["c"] && fs.existsSync(config["c"])) {
71
+ repos = getRepositories(config["c"]);
72
+ directory = path.dirname(config["c"]);
73
+ console.warn(
74
+ `${color.yellow}using ${config["c"]} configuration${color.reset}`
75
+ );
76
+ }
77
+ // Load repositories from default CoCreate.config.js if exists
78
+ else if (!config["self"] && fs.existsSync(currentRepoPath)) {
79
+ repos = getRepositories(currentRepoPath);
80
+ directory = path.dirname(currentRepoPath);
81
+ console.warn(
82
+ `${color.yellow}using ${currentRepoPath} configuration${color.reset}`
83
+ );
84
+ }
85
+ // If package.json exists, load repository details from it
86
+ else if (fs.existsSync(packageJsonPath)) {
87
+ const repoPath = path.resolve(process.cwd());
88
+ const packageObj = require(packageJsonPath);
89
+ const repoUrl =
90
+ packageObj.repository &&
91
+ packageObj.repository.url.substring(12);
92
+ const repoEntry = packageObj.main;
93
+ repos = [
94
+ {
95
+ path: repoPath,
96
+ repo: repoUrl,
97
+ entry: repoEntry
98
+ }
99
+ ];
100
+ directory = path.dirname(packageJsonPath);
101
+ console.warn(
102
+ `${color.yellow}using ${packageJsonPath} configuration${color.reset}`
103
+ );
104
+ }
105
+ // Error if no configuration can be found
106
+ else {
107
+ console.error(
108
+ `${color.red}a configuration file cannot be found${color.reset}`
109
+ );
110
+ process.exit(1);
111
+ }
112
+ }
113
+
114
+ // Set default config values
115
+ config = { hideMessage: false, ...config };
116
+
117
+ // Add metadata to repos if any are present
118
+ if (repos && repos.length) repos = await addMeta(repos, [], directory);
119
+
120
+ // Execute the command across repositories
121
+ const failed = await execute(command, repos, config);
122
+
123
+ // Handle any failed command executions
124
+ if (failed && failed.length > 0) {
125
+ console.log(
126
+ color.red +
127
+ " **************** failures **************** " +
128
+ color.reset
129
+ );
130
+ for (const failure of failed) {
131
+ console.log(
132
+ color.red + `${failure.name}: ${failure.error}` + color.reset
133
+ );
134
+ }
135
+
136
+ // Prompt user to retry failed commands
137
+ await promptRetry(failed, config, directory);
138
+ }
139
+ }
140
+
141
+ /**
142
+ * Prompt the user to retry failed commands.
143
+ * @param {Array} failed - List of failed commands.
144
+ * @param {Object} config - Configuration object.
145
+ * @param {string} directory - Path of the configuration directory.
146
+ */
147
+ async function promptRetry(failed, config, directory) {
148
+ const rl = readline.createInterface({
149
+ input: process.stdin,
150
+ output: process.stdout
151
+ });
152
+
153
+ rl.question(
154
+ "Do you want to retry the failed commands? (yes/no): ",
155
+ async (answer) => {
156
+ rl.close();
157
+ if (answer.toLowerCase() === "yes") {
158
+ await main(config, failed, directory);
159
+ } else {
160
+ process.exit(0);
161
+ }
162
+ }
163
+ );
164
+ }
165
+
166
+ // Call the main function with initial configuration
167
+ main(config);
@@ -1,25 +1,22 @@
1
- const { requestCertificate } = require('@cocreate/acme')
1
+ const { requestCertificate } = require("@cocreate/acme");
2
2
 
3
3
  module.exports = async function nginx(repos, args) {
4
- let failed = [];
5
-
6
- try {
7
- if (args.length) {
8
- if (args[0] === 'create') {
9
- args.shift()
10
- await createServer(args);
11
- } else if (args[0] === 'delete') {
12
- args.shift()
13
- await deleteServer(args);
14
- } else
15
- await createServer(args);
16
- }
17
- } catch (err) {
18
- failed.push({ name: 'GENERAL', des: err.message });
19
- console.error(err.red);
20
- } finally {
21
- return failed;
22
- }
23
-
24
- }
4
+ let failed = [];
25
5
 
6
+ try {
7
+ if (args.length) {
8
+ if (args[0] === "create") {
9
+ args.shift();
10
+ await createServer(args);
11
+ } else if (args[0] === "delete") {
12
+ args.shift();
13
+ await deleteServer(args);
14
+ } else await createServer(args);
15
+ }
16
+ } catch (err) {
17
+ failed.push({ name: "GENERAL", error: err.message });
18
+ console.error(err.red);
19
+ } finally {
20
+ return failed;
21
+ }
22
+ };