@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.
@@ -1,191 +1,185 @@
1
- let cdnUrl = "https://server.cocreate.app/";
1
+ const fs = require("fs");
2
+ const path = require("path");
3
+ const { execSync } = require("child_process");
2
4
 
5
+ module.exports = async function generateWebpackConfigs(repos, args) {
6
+ console.log("Generating webpack configs for specified repositories...");
3
7
 
8
+ if (!Array.isArray(repos) || repos.length === 0) {
9
+ console.warn("No repositories specified.");
10
+ return [];
11
+ }
4
12
 
5
- let glob = require("glob");
6
- let fs = require('fs');
7
- const prettier = require("prettier");
8
- const path = require("path")
9
-
10
- function globUpdater(er, files) {
11
- if (er)
12
- console.log(files, 'glob resolving issue')
13
- else
14
- files.forEach(filename => update(filename))
15
- }
16
-
17
-
18
-
19
-
20
- function update(webpackPath) {
21
-
13
+ const failed = [];
22
14
 
15
+ for (const repo of repos) {
16
+ if (!repo || typeof repo.path !== "string") {
17
+ repo.error = "Invalid repository data provided";
18
+ failed.push(repo);
19
+ continue;
20
+ }
23
21
 
24
- let dir = path.dirname(webpackPath);
22
+ const result = updateWebpackConfigForModule(repo);
23
+ if (!result) {
24
+ repo.error = `${repo.path}/webpack.config.js`;
25
+ failed.push(repo);
26
+ } else {
27
+ console.log(`Generated: ${repo.path}/webpack.config.js`);
28
+ }
29
+ }
25
30
 
26
- // component name
27
- let name = path.basename(dir);
28
- // component entry
29
-
30
-
31
- let entry;
32
-
33
- if (fs.existsSync( path.resolve(dir, './src/index.js') ))
34
- entry = "./src/index.js";
35
- else
36
- entry = './src/' + name + '.js';
31
+ console.log(`\nWebpack config generation process finished.`);
37
32
 
38
- // get component name in came case "cocreate" less
39
- let componentName = toCamelCase(name);
40
- if (componentName.startsWith('CoCreate'))
41
- componentName = componentName.substring(8);
33
+ return failed;
34
+ };
42
35
 
43
- if (componentName === componentName.toUpperCase())
44
- componentName = componentName.toLowerCase();
45
- else
46
- componentName = lowerCaseFirstChar(componentName)
36
+ function updateWebpackConfigForModule(repo) {
37
+ try {
38
+ repo.path = path.resolve(process.cwd(), repo.path);
47
39
 
40
+ if (!fs.existsSync(repo.path) || !fs.lstatSync(repo.path).isDirectory()) {
41
+ repo.error = `The path "${repo.path}" does not exist or is not a directory.`;
42
+ return false;
43
+ }
48
44
 
49
- // has template to inject script and styles on dev compile
50
- let hasTemplate = false;
51
- if (fs.existsSync(path.resolve(dir, './src/index.html')))
52
- hasTemplate = true;
53
- let fileContent = `const path = require("path");
54
- const TerserPlugin = require("terser-webpack-plugin");
55
- const MiniCssExtractPlugin = require('mini-css-extract-plugin');
56
- let isProduction = process.env.NODE_ENV === "production";
57
- const { CleanWebpackPlugin } = require("clean-webpack-plugin");
58
- ${ hasTemplate ? "const HtmlWebpackPlugin = require('html-webpack-plugin');" : ''}
45
+ const webpackPath = path.resolve(repo.path, "webpack.config.js");
46
+ let name = path.basename(repo.path);
59
47
 
48
+ if (!fs.existsSync(path.resolve(repo.path, repo.entry))) {
49
+ repo.error = `Entry file ${repo.entry} does not exist in ${repo.path}`;
50
+ return false;
51
+ }
60
52
 
53
+ let componentName = toCamelCase(name);
54
+ if (componentName === componentName.toUpperCase()) {
55
+ componentName = componentName.toLowerCase();
56
+ } else {
57
+ componentName = lowerCaseFirstChar(componentName);
58
+ }
61
59
 
60
+ let hasTemplate = false;
61
+ if (fs.existsSync(path.resolve(repo.path, "./src/index.html"))) {
62
+ hasTemplate = true;
63
+ }
64
+ let fileContent = `
65
+ const path = require("path");
66
+ const MiniCssExtractPlugin = require("mini-css-extract-plugin");
67
+ const { EsbuildPlugin } = require("esbuild-loader");
68
+ const { FileUploader } = require("@cocreate/webpack");
69
+ ${
70
+ hasTemplate ? 'const HtmlWebpackPlugin = require("html-webpack-plugin");' : ""
71
+ }
62
72
 
63
- module.exports = {
64
- entry: {
65
- '${name}': '${entry}'
66
- },
67
- output: {
68
- path: path.resolve(__dirname, 'dist'),
69
- filename: isProduction ? '[name].min.js' : '[name]${hasTemplate ? '[hash]': ''}.js',
70
- libraryTarget: 'umd',
71
- libraryExport: 'default',
72
- library: ${ componentName ? `['CoCreate', '${componentName}']` : 'CoCreate'},
73
- globalObject: "this",
74
- },
75
-
76
-
77
- plugins: [new CleanWebpackPlugin(),
73
+ module.exports = async (env, argv) => {
74
+ const isProduction = argv && argv.mode === "production";
75
+ const config = {
76
+ entry: {
77
+ "${name}": "${repo.entry}"
78
+ },
79
+ output: {
80
+ path: path.resolve(__dirname, "dist"),
81
+ filename: isProduction ? "[name].min.js" : "[name]${
82
+ hasTemplate ? ".[contenthash]" : ""
83
+ }.js",
84
+ libraryExport: "default",
85
+ library: ["CoCreate", "${componentName}"],
86
+ clean: true
87
+ },
88
+ plugins: [
78
89
  new MiniCssExtractPlugin({
79
- filename: '[name].css',
80
- }),
81
- ${ hasTemplate ? `
90
+ filename: isProduction ? "[name].min.css" : "[name].css",
91
+ }),
92
+ new FileUploader(env, argv),
93
+ ${
94
+ hasTemplate
95
+ ? `
82
96
  new HtmlWebpackPlugin({
83
97
  template: "./src/index.html"
84
98
  })
85
- ` : ''}
99
+ `
100
+ : ""
101
+ }
86
102
  ],
87
- // Default mode for Webpack is production.
88
- // Depending on mode Webpack will apply different things
89
- // on final bundle. For now we don't need production's JavaScript
90
- // minifying and other thing so let's set mode to development
91
- mode: isProduction ? "production" : "development",
92
- module: {
93
- rules: [
94
- {
95
- test: /\.js$/,
96
- exclude: /(node_modules)/,
97
- use: {
98
- loader: "babel-loader",
99
- options: {
100
- plugins: ["@babel/plugin-transform-modules-commonjs"],
103
+ mode: isProduction ? "production" : "development",
104
+ devtool: isProduction ? "source-map" : "eval-source-map",
105
+ module: {
106
+ rules: [
107
+ {
108
+ test: /\.js$/,
109
+ exclude: /node_modules/,
110
+ use: {
111
+ loader: "esbuild-loader",
112
+ options: {
113
+ loader: "js",
114
+ target: "es2017"
115
+ }
101
116
  },
102
117
  },
103
- },
104
- {
105
- test: /\.css$/i,
106
- use: [
107
- { loader: "style-loader", options: { injectType: "linkTag" } },
108
- "file-loader",
109
- ],
110
- },
111
- ],
112
- },
113
-
114
-
115
- // add source map
116
- ...(isProduction ? {} : { devtool: 'eval-source-map' }),
117
-
118
- optimization: {
119
- minimize: true,
120
- minimizer: [
121
- new TerserPlugin({
122
- extractComments: true,
123
- // cache: true,
124
- parallel: true,
125
- // sourceMap: true, // Must be set to true if using source-maps in production
126
- terserOptions: {
127
- // https://github.com/webpack-contrib/terser-webpack-plugin#terseroptions
128
- // extractComments: 'all',
129
- compress: {
130
- drop_console: true,
131
- },
118
+ {
119
+ test: /\.css$/i,
120
+ use: [MiniCssExtractPlugin.loader, "css-loader"]
132
121
  },
133
- }),
134
- ],
122
+ ],
123
+ },
124
+ optimization: {
125
+ minimize: isProduction,
126
+ minimizer: [
127
+ new EsbuildPlugin({
128
+ target: "es2017",
129
+ css: true
130
+ })
131
+ ],
135
132
  splitChunks: {
136
- chunks: 'all',
137
- minSize: 200,
138
- // maxSize: 99999,
139
- //minChunks: 1,
140
- ${ !hasTemplate ? `
141
133
  cacheGroups: {
142
134
  defaultVendors: false
143
135
  },
144
- ` : ''}
145
-
146
136
  }
147
- },
148
- }
137
+ },
138
+ performance: {
139
+ hints: isProduction ? "warning" : false
140
+ }
141
+ };
142
+ return config;
143
+ };
149
144
  `;
150
- let formated = prettier.format(fileContent, { semi: false, parser: "babel" });
151
-
152
- if (fs.existsSync(webpackPath))
153
- fs.unlinkSync(webpackPath)
154
- fs.writeFileSync(webpackPath, formated)
155
- // console.log(fileContent)
156
- // process.exit()
157
-
145
+ fs.writeFileSync(webpackPath, fileContent);
146
+ execSync(`npx prettier --write "${webpackPath}"`).toString();
147
+ return true;
148
+ } catch (error) {
149
+ return false;
150
+ }
158
151
  }
159
152
 
160
-
161
-
162
- glob("../CoCreate-components/*/webpack.config.js", globUpdater)
163
- glob("../CoCreate-apps/*/webpack.config.js", globUpdater)
164
- glob("../CoCreate-plugins/*/webpack.config.js", globUpdater)
165
- glob("../CoCreateCSS/webpack.config.js", globUpdater)
166
-
167
-
168
-
169
-
170
-
171
153
  function toCamelCase(str) {
172
154
  let index = 0;
155
+ str = str.replace(/^CoCreate-/, "");
173
156
  do {
174
157
  index = str.indexOf("-", index);
175
158
  if (index !== -1) {
176
159
  let t = str.substring(0, index);
177
- t += String.fromCharCode(str.charCodeAt(index + 1) - 32);
178
- t += str.substring(index + 2);
179
- str = t;
180
- }
181
- else break;
160
+ if (
161
+ index + 1 < str.length &&
162
+ str[index + 1] >= "a" &&
163
+ str[index + 1] <= "z"
164
+ ) {
165
+ t += String.fromCharCode(str.charCodeAt(index + 1) - 32);
166
+ t += str.substring(index + 2);
167
+ str = t;
168
+ } else if (index + 1 < str.length) {
169
+ t += str.substring(index + 1);
170
+ str = t;
171
+ } else {
172
+ str = t;
173
+ }
174
+ } else break;
182
175
  } while (true);
183
176
  return str;
184
177
  }
185
178
 
186
179
  function lowerCaseFirstChar(str) {
187
- return String.fromCharCode(str.charCodeAt(0) + 32) + str.substring(1);
180
+ if (!str) return str;
181
+ if (str.charCodeAt(0) >= 65 && str.charCodeAt(0) <= 90) {
182
+ return String.fromCharCode(str.charCodeAt(0) + 32) + str.substring(1);
183
+ }
184
+ return str;
188
185
  }
189
-
190
-
191
- //
@@ -1,25 +1,21 @@
1
- const addMeta = require('../addMeta');
1
+ const addMeta = require("../addMeta");
2
2
 
3
- module.exports = async function(repos, args) {
4
- let failed = [];
5
- try {
6
- let cloneFailed = await require('./clone.js')(repos, args)
7
- if (cloneFailed)
8
- failed.push(cloneFailed)
3
+ module.exports = async function (repos, args) {
4
+ let failed = [];
5
+ try {
6
+ let cloneFailed = await require("./clone.js")(repos, args);
7
+ if (cloneFailed) failed.push(cloneFailed);
9
8
 
10
- repos = await addMeta(repos, failed)
9
+ repos = await addMeta(repos, failed);
11
10
 
12
- let symlinkFailed = await require('./symlink.js')(repos, args)
13
- if (symlinkFailed)
14
- failed.push(symlinkFailed)
15
-
16
- let linkFailed = await require('./link.js')(repos, args)
17
- if (linkFailed)
18
- failed.push(linkFailed)
19
-
20
- } catch (err) {
21
- console.error(err);
22
- failed.push({ name: 'general', des: err.message })
23
- }
24
- return failed;
25
- }
11
+ let symlinkFailed = await require("./symlink.js")(repos, args);
12
+ if (symlinkFailed) failed.push(symlinkFailed);
13
+
14
+ let linkFailed = await require("./link.js")(repos, args);
15
+ if (linkFailed) failed.push(linkFailed);
16
+ } catch (err) {
17
+ console.error(err);
18
+ failed.push({ name: "general", error: err.message });
19
+ }
20
+ return failed;
21
+ };
@@ -1,69 +1,82 @@
1
- const spawn = require('../spawn');
2
- const path = require("path")
3
- const fs = require('fs')
4
- const { color } = require('../fonts');
1
+ const spawn = require("../spawn");
2
+ const path = require("path");
3
+ const fs = require("fs");
4
+ const { color } = require("../fonts");
5
5
 
6
6
  module.exports = async (repos, args) => {
7
- const failed = [], isLinked = {};
7
+ const failed = [],
8
+ isLinked = {};
8
9
 
9
- try {
10
- for (let repo of repos) {
11
- if (!repo) continue;
12
- if (repo.exclude && repo.exclude.includes('link'))
13
- continue
10
+ try {
11
+ for (let repo of repos) {
12
+ if (!repo) continue;
13
+ if (repo.exclude && repo.exclude.includes("link")) continue;
14
14
 
15
- if (process.cwd() === repo.absolutePath)
16
- continue
15
+ if (process.cwd() === repo.absolutePath) continue;
17
16
 
18
- if (repo.packageManager === 'npm') {
19
- let dir = path.resolve(process.cwd(), 'node_modules');
20
- let dest = path.resolve(path.resolve(repo.absolutePath), 'node_modules');
21
- if (dir && dest) {
22
- if (fs.existsSync(dest))
23
- await fs.promises.rm(dest, { recursive: true, force: true });
17
+ if (repo.packageManager === "npm") {
18
+ let dir = path.resolve(process.cwd(), "node_modules");
19
+ let dest = path.resolve(
20
+ path.resolve(repo.absolutePath),
21
+ "node_modules"
22
+ );
23
+ if (dir && dest) {
24
+ if (fs.existsSync(dest))
25
+ await fs.promises.rm(dest, {
26
+ recursive: true,
27
+ force: true
28
+ });
24
29
 
25
- await fs.promises.symlink(dir, dest, 'dir');
26
- console.log(repo.packageManager, 'link', repo.packageName)
27
- }
28
- } else {
30
+ await fs.promises.symlink(dir, dest, "dir");
31
+ console.log(repo.packageManager, "link", repo.packageName);
32
+ }
33
+ } else {
34
+ let exitCode = await spawn(repo.packageManager, ["link"], {
35
+ cwd: repo.absolutePath,
36
+ shell: true,
37
+ stdio: "inherit"
38
+ });
29
39
 
30
- let exitCode = await spawn(repo.packageManager, ['link'], {
31
- cwd: repo.absolutePath,
32
- shell: true,
33
- stdio: 'inherit'
34
- });
40
+ if (exitCode !== 0) {
41
+ failed.push({
42
+ name: repo.name,
43
+ error: `${repo.packageManager} link failed`
44
+ });
45
+ console.error(
46
+ color.red +
47
+ `${repo.name}: ${repo.packageManager} link failed` +
48
+ color.reset
49
+ );
50
+ } else {
51
+ console.log(repo.packageManager, "link", repo.packageName);
35
52
 
36
- if (exitCode !== 0) {
37
- failed.push({
38
- name: repo.name,
39
- des: `${repo.packageManager} link failed`
40
- })
41
- console.error(color.red + `${repo.name}: ${repo.packageManager} link failed` + color.reset)
42
- } else {
43
- console.log(repo.packageManager, 'link', repo.packageName)
53
+ let exitCode = await spawn(
54
+ repo.packageManager,
55
+ ["link", repo.packageName],
56
+ {
57
+ cwd: process.cwd(),
58
+ shell: true,
59
+ stdio: "inherit"
60
+ }
61
+ );
62
+ if (exitCode !== 0) {
63
+ failed.push({
64
+ name: repo.name,
65
+ error: `${repo.packageManager} link ${repo.packageName} failed`
66
+ });
67
+ console.error(
68
+ color.red +
69
+ `${repo.name}: ${repo.packageManager} link ${repo.packageName} failed` +
70
+ color.reset
71
+ );
72
+ }
73
+ }
74
+ }
75
+ }
76
+ } catch (err) {
77
+ failed.push({ name: "GENERAL", error: err.message });
78
+ console.error(color.red + `${err}` + color.reset);
79
+ }
44
80
 
45
- let exitCode = await spawn(repo.packageManager, ['link', repo.packageName], {
46
- cwd: process.cwd(),
47
- shell: true,
48
- stdio: 'inherit'
49
- })
50
- if (exitCode !== 0) {
51
- failed.push({
52
- name: repo.name,
53
- des: `${repo.packageManager} link ${repo.packageName} failed`
54
- });
55
- console.error(color.red + `${repo.name}: ${repo.packageManager} link ${repo.packageName} failed` + color.reset)
56
- }
57
- }
58
-
59
- }
60
- }
61
-
62
- }
63
- catch (err) {
64
- failed.push({ name: 'GENERAL', des: err.message })
65
- console.error(color.red + `${err}` + color.reset)
66
- }
67
-
68
- return failed;
69
- }
81
+ return failed;
82
+ };
@@ -1,25 +1,22 @@
1
- const { createServer, deleteServer } = require('@cocreate/nginx')
1
+ const { createServer, deleteServer } = require("@cocreate/nginx");
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
+ };