@cocreate/cli 1.48.0 → 1.50.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,130 @@
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
+ const path = require("path");
3
+ const fs = require("fs");
4
+ const readline = require("readline");
5
+ const execute = require("./execute");
6
+ const addMeta = require("./addMeta");
7
+ const { color } = require("./fonts");
8
+
9
+ let config = {};
10
+
11
+ const argv = process.argv.slice(2);
12
+ const options = ["-self"];
13
+ for (let option of options) {
14
+ if (argv.includes(option)) {
15
+ config[option.replace(/^--/, "")] = true;
16
+ const index = argv.indexOf(option);
17
+ delete argv[index];
18
+ }
19
+ }
20
+
21
+ command = argv
22
+ .map((part) =>
23
+ part.match(/ |'|"/) ? `'${part.replace(/'/, "\\'")}'` : part
24
+ )
25
+ .join(" ");
26
+
27
+ function getRepositories(path) {
28
+ try {
29
+ const config = require(path);
30
+ return config.repositories;
31
+ } catch (err) {
32
+ console.error(
33
+ color.red + "cannot read repository file in" + color.reset,
34
+ path,
35
+ color.red + "error:" + color.reset,
36
+ err.message
37
+ );
38
+ process.exit(1);
39
+ }
40
+ }
41
+
42
+ async function main(config = {}, repos = null, directory = null) {
43
+ if (!repos) {
44
+ // Existing logic to determine repositories and configuration
45
+ const currentRepoPath = path.resolve(
46
+ process.cwd(),
47
+ "CoCreate.config.js"
48
+ );
49
+ const packageJsonPath = path.resolve(process.cwd(), "package.json");
50
+
51
+ if (config["c"] && fs.existsSync(config["c"])) {
52
+ repos = getRepositories(config["c"]);
53
+ directory = path.dirname(config["c"]);
54
+ console.warn(
55
+ `${color.yellow}using ${config["c"]} configuration${color.reset}`
56
+ );
57
+ } else if (!config["self"] && fs.existsSync(currentRepoPath)) {
58
+ repos = getRepositories(currentRepoPath);
59
+ directory = path.dirname(currentRepoPath);
60
+ console.warn(
61
+ `${color.yellow}using ${currentRepoPath} configuration${color.reset}`
62
+ );
63
+ } else if (fs.existsSync(packageJsonPath)) {
64
+ const repoPath = path.resolve(process.cwd());
65
+ const packageObj = require(packageJsonPath);
66
+ const repoUrl =
67
+ packageObj.repository &&
68
+ packageObj.repository.url.substring(12);
69
+ const repoEntry = packageObj.main;
70
+ repos = [
71
+ {
72
+ path: repoPath,
73
+ repo: repoUrl,
74
+ entry: repoEntry
75
+ }
76
+ ];
77
+ directory = path.dirname(packageJsonPath);
78
+ console.warn(
79
+ `${color.yellow}using ${packageJsonPath} configuration${color.reset}`
80
+ );
81
+ } else {
82
+ console.error(
83
+ `${color.red}a configuration file cannot be found${color.reset}`
84
+ );
85
+ process.exit(1);
86
+ }
87
+ }
88
+
89
+ config = { hideMessage: false, ...config };
90
+
91
+ if (repos && repos.length) repos = await addMeta(repos, [], directory);
92
+
93
+ const failed = await execute(command, repos, config);
94
+
95
+ if (failed && failed.length > 0) {
96
+ console.log(
97
+ color.red +
98
+ " **************** failures **************** " +
99
+ color.reset
100
+ );
101
+ for (const failure of failed) {
102
+ console.log(
103
+ color.red + `${failure.name}: ${failure.error}` + color.reset
104
+ );
105
+ }
106
+
107
+ await promptRetry(failed, config, directory);
108
+ }
109
+ }
110
+
111
+ async function promptRetry(failed, config, directory) {
112
+ const rl = readline.createInterface({
113
+ input: process.stdin,
114
+ output: process.stdout
115
+ });
116
+
117
+ rl.question(
118
+ "Do you want to retry the failed commands? (yes/no): ",
119
+ async (answer) => {
120
+ rl.close();
121
+ if (answer.toLowerCase() === "yes") {
122
+ await main(config, failed, directory);
123
+ } else {
124
+ process.exit(0);
125
+ }
126
+ }
127
+ );
128
+ }
129
+
130
+ 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
+ };