@i2analyze/create-connector 2.0.0-next.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/CHANGELOG.md ADDED
@@ -0,0 +1,33 @@
1
+ # @i2analyze/create-connector
2
+
3
+ ## 2.0.0-next.0
4
+
5
+ ### Major Changes
6
+
7
+ - 07d9ec3: BREAKING CHANGE: Removed "--no-connectors" argument when creating new connector server
8
+
9
+ ## 1.0.3
10
+
11
+ ### Patch Changes
12
+
13
+ - cc9d526: Update copyright information.
14
+ - ed28e8e: Update urls to use correct github organization.
15
+
16
+ ## 1.0.2
17
+
18
+ ### Patch Changes
19
+
20
+ - 927589d: Update READMEs and homepages for public packages
21
+
22
+ ## 1.0.1
23
+
24
+ ### Patch Changes
25
+
26
+ - 358415b: Use a `^` dependency for i2connect-scripts.
27
+ - e46293f: Rename `sample` to `skeleton` in the template, and change casing to better reflect the nature of the files produced.
28
+
29
+ ## 1.0.0
30
+
31
+ ### Major Changes
32
+
33
+ - 79b1db9: Initial release.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # @i2analyze/create-connector
2
+
3
+ This package is part of the [i2 Connect SDK](https://github.com/i2group/analyze-connect-node-sdk).
package/index.js ADDED
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env node
2
+ /*!
3
+ * Copyright 2022- N. Harris Computer Corporation. All rights reserved
4
+ * SPDX-License-Identifier: MIT
5
+ */
6
+
7
+ 'use strict';
8
+
9
+ var thisVersion = process.versions.node;
10
+ var thisMajor = thisVersion.split('.')[0];
11
+ var minimumMajorRequired = 14;
12
+
13
+ if (thisMajor < minimumMajorRequired) {
14
+ console.error(
15
+ 'Node ' +
16
+ minimumMajorRequired +
17
+ ' or higher is required. You are using ' +
18
+ thisVersion +
19
+ '. Update your version of Node.'
20
+ );
21
+ process.exit(5);
22
+ }
23
+
24
+ var { main } = require('./main');
25
+ main();
package/main.js ADDED
@@ -0,0 +1,211 @@
1
+ /*!
2
+ * Copyright 2022- N. Harris Computer Corporation. All rights reserved
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ const commander = require('commander');
7
+ const chalk = require('chalk');
8
+ const fileUriToPath = require('file-uri-to-path');
9
+ const fs = require('fs');
10
+ const path = require('path');
11
+ const spawn = require('cross-spawn');
12
+ const fileUrl = require('file-url');
13
+
14
+ const thisPackageJson = require('./package.json');
15
+
16
+ exports.main = async function main() {
17
+ /** @type string */
18
+ let projectDirOrName;
19
+
20
+ const program = new commander.Command(thisPackageJson.name)
21
+ .version(thisPackageJson.version)
22
+ .option('--i2connect-version <version>', 'the version of the i2connect package to use', 'latest')
23
+ .option('--i2connect-scripts-version <version>', 'the version of i2connect-scripts to use', 'latest')
24
+ .arguments('<project-directory>')
25
+ .usage(`${chalk.green('<project-directory>')} [options]`)
26
+ .action((name) => {
27
+ projectDirOrName = name;
28
+ })
29
+ .on('--help', () => {
30
+ console.log();
31
+ })
32
+ .parse(process.argv);
33
+
34
+ if (typeof projectDirOrName === 'undefined') {
35
+ console.error(`Specify a project directory.`);
36
+ console.log(` ${chalk.cyan(program.name())} ${chalk.green('<project-directory>')}`);
37
+ process.exit(5);
38
+ }
39
+
40
+ const projectDir = path.resolve(process.cwd(), projectDirOrName);
41
+ const projectName = path.basename(projectDir);
42
+
43
+ const packageJson = {
44
+ name: projectName,
45
+ version: '0.1.0',
46
+ private: true,
47
+ };
48
+
49
+ console.log(`Creating a new i2 Connect server in ${chalk.green(projectDir)}.`);
50
+
51
+ try {
52
+ const stat = await fs.promises.stat(projectDir);
53
+ // Oh dear. Target already exists.
54
+ console.error(chalk.red(`ERROR: Target folder ${projectDir} already exists.`));
55
+ process.exit(5);
56
+ } catch (_e) {
57
+ // Target does not exist. Good.
58
+ }
59
+
60
+ await fs.promises.mkdir(projectDir);
61
+ await fs.promises.writeFile(path.join(projectDir, 'package.json'), JSON.stringify(packageJson, null, 2), 'utf8');
62
+ process.chdir(projectDir);
63
+
64
+ const developmentLocation = process.env.I2CONNECT_DEVELOPMENT_OVERRIDE;
65
+
66
+ const { i2connectScriptsVersion, i2connectVersion } = program.opts();
67
+
68
+ function findPackage(officialPackageName, localDirectoryName) {
69
+ if (developmentLocation) {
70
+ return fileUrl(developmentLocation) + '/packages/' + localDirectoryName;
71
+ } else {
72
+ return officialPackageName;
73
+ }
74
+ }
75
+
76
+ const scriptsPackage = findPackage('@i2analyze/i2connect-scripts', 'i2connect-scripts');
77
+ const scriptsPackageName = getPackageInfo(scriptsPackage);
78
+
79
+ let restoreDevelopmentPackage = () => {};
80
+
81
+ if (developmentLocation) {
82
+ // This message is for developers of this SDK package, not end users.
83
+ console.warn(`i2 Connect development mode: Loading scripts package from ${scriptsPackage}.`);
84
+ const scriptsPackageJson = path.join(developmentLocation, 'packages', 'i2connect-scripts', 'package.json');
85
+
86
+ // When we 'install' a local development version of i2connect-scripts, npm will follow
87
+ // the @i2analyze/eslint-config-i2connect dependency, trying to install a version
88
+ // that may not have been published. (It doesn't understand workspaces, i.e. that
89
+ // we already have the package.) So, just before we 'install' the local i2connect-scripts,
90
+ // we patch its package.json dependency section to point the eslint-config dependency
91
+ // explicitly at the local path. (We put it back again in the finally block.)
92
+
93
+ const originalPackageJsonString = await fs.promises.readFile(scriptsPackageJson, 'utf8');
94
+ const packageJson = JSON.parse(originalPackageJsonString);
95
+ packageJson.dependencies['@i2analyze/eslint-config-i2connect'] = findPackage(
96
+ '@i2analyze/eslint-config-i2connect',
97
+ 'eslint-config-i2connect'
98
+ );
99
+ await fs.promises.writeFile(scriptsPackageJson, JSON.stringify(packageJson, null, 2), 'utf8');
100
+ restoreDevelopmentPackage = async () => {
101
+ await fs.promises.writeFile(scriptsPackageJson, originalPackageJsonString, 'utf8');
102
+ };
103
+ }
104
+
105
+ const versionedScriptsPackage =
106
+ i2connectScriptsVersion && !developmentLocation ? `${scriptsPackage}@${i2connectScriptsVersion}` : scriptsPackage;
107
+
108
+ let installFailed = false;
109
+
110
+ try {
111
+ await install(projectDir, false, true, [versionedScriptsPackage]);
112
+ } catch (_e) {
113
+ installFailed = true;
114
+ } finally {
115
+ await restoreDevelopmentPackage();
116
+ }
117
+
118
+ if (installFailed) {
119
+ console.error(chalk.red(`ERROR: Failed to install ${versionedScriptsPackage}.`));
120
+ process.exit(5);
121
+ }
122
+
123
+ /**
124
+ * @type {import("@i2analyze/i2connect-scripts/src/init").IInitParams}
125
+ */
126
+ const initParams = {
127
+ projectPath: projectDir,
128
+ i2connectVersion: developmentLocation ? undefined : i2connectVersion,
129
+ };
130
+
131
+ // Pass control over to the init.js file in the i2connect-scripts package.
132
+ try {
133
+ await runNode(
134
+ `var init = require('${scriptsPackageName}/dist/cjs/init.js'); init.run(...JSON.parse(process.argv[1]));`,
135
+ [initParams],
136
+ process.cwd()
137
+ );
138
+ } catch (_e) {
139
+ // Errors will already be reported by init. We don't want to report messy unhandled
140
+ // exceptions _after_ init has emitted clean errors.
141
+ }
142
+ };
143
+
144
+ /**
145
+ * Given a package specifier (a package name or a file: URL),
146
+ * returns the simple name of the package.
147
+ * @param {string} pkg
148
+ */
149
+ function getPackageInfo(pkg) {
150
+ if (pkg.startsWith('file:')) {
151
+ const pkgInfo = require(path.join(fileUriToPath(pkg), 'package.json'));
152
+ return pkgInfo.name;
153
+ }
154
+
155
+ return pkg;
156
+ }
157
+
158
+ function runNode(script, data, cwd) {
159
+ const serializedData = JSON.stringify(data);
160
+ const child = spawn(process.execPath, ['-e', script, '--', serializedData], {
161
+ cwd,
162
+ stdio: 'inherit',
163
+ });
164
+
165
+ return waitForChildProcess(child, 'node', data);
166
+ }
167
+
168
+ /**
169
+ * @param {string} rootDir
170
+ * @param {string} useYarn
171
+ * @param {boolean} dependenciesAreDevelopment
172
+ * @param {string} dependencies
173
+ * @returns Promise<void>
174
+ */
175
+ function install(rootDir, useYarn, dependenciesAreDevelopment, dependencies) {
176
+ /** @type string */
177
+ let command;
178
+
179
+ /** @type string[] */
180
+ let args = [];
181
+
182
+ if (useYarn) {
183
+ command = 'yarn';
184
+ args = ['add', '--exact'];
185
+ if (dependenciesAreDevelopment) {
186
+ args.push('-D');
187
+ }
188
+ args.push(...dependencies);
189
+ } else {
190
+ command = 'npm';
191
+ args = ['install', dependenciesAreDevelopment ? '--save-dev' : '--save', '--loglevel', 'error', ...dependencies];
192
+ }
193
+
194
+ const child = spawn(command, args, { stdio: 'inherit', cwd: rootDir });
195
+
196
+ return waitForChildProcess(child, command, args);
197
+ }
198
+
199
+ function waitForChildProcess(child, command, args) {
200
+ return new Promise((resolve, reject) => {
201
+ child.on('close', (code) => {
202
+ if (code !== 0) {
203
+ reject({
204
+ command: `${command} ${args.join(' ')}`,
205
+ });
206
+ return;
207
+ }
208
+ resolve();
209
+ });
210
+ });
211
+ }
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@i2analyze/create-connector",
3
+ "version": "2.0.0-next.0",
4
+ "license": "MIT",
5
+ "publishConfig": {
6
+ "access": "public",
7
+ "registry": "https://registry.npmjs.org/"
8
+ },
9
+ "homepage": "https://github.com/i2group/analyze-connect-node-sdk",
10
+ "bin": "index.js",
11
+ "files": [
12
+ "index.js",
13
+ "main.js"
14
+ ],
15
+ "devDependencies": {
16
+ "@i2analyze/i2connect-scripts": "2.0.0-next.5",
17
+ "@types/node-fetch": "^2.5.12",
18
+ "node-fetch": "^2.6.1"
19
+ },
20
+ "dependencies": {
21
+ "chalk": "^4.1.0",
22
+ "commander": "^8.0.0",
23
+ "cross-spawn": "^7.0.3",
24
+ "file-uri-to-path": "^2.0.0",
25
+ "file-url": "^3.0.0"
26
+ },
27
+ "engines": {
28
+ "node": ">=14"
29
+ },
30
+ "volta": {
31
+ "extends": "../../package.json"
32
+ },
33
+ "scripts": {
34
+ "lint": "eslint .",
35
+ "test": "node ./test.js"
36
+ }
37
+ }