scripting-cli 1.2.0 → 1.2.1

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/dist/const.js ADDED
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.scriptingDtsFileName = exports.globalDtsFileName = void 0;
4
+ exports.globalDtsFileName = 'global.d.ts';
5
+ exports.scriptingDtsFileName = 'scripting.d.ts';
@@ -9,6 +9,7 @@ const util_1 = require("./util");
9
9
  const fs_1 = __importDefault(require("fs"));
10
10
  const chalk_1 = __importDefault(require("chalk"));
11
11
  const path_1 = __importDefault(require("path"));
12
+ const const_1 = require("./const");
12
13
  class Controller {
13
14
  static instances = new Map();
14
15
  static create(socket, noAutoOpen) {
@@ -43,7 +44,7 @@ class Controller {
43
44
  });
44
45
  fs_1.default.promises.readdir((0, util_1.getScriptPath)()).then((files) => {
45
46
  const serverScriptNames = files.filter((filename) => fs_1.default.statSync((0, util_1.getScriptPath)(filename)).isDirectory()
46
- && filename !== '.vscode'
47
+ && !filename.startsWith('.')
47
48
  && filename !== 'dts');
48
49
  socket.emit("serverScripts", serverScriptNames);
49
50
  });
@@ -107,8 +108,8 @@ class Controller {
107
108
  this.watcher = null;
108
109
  }
109
110
  await (0, util_1.writeDtsFiles)({
110
- "global.d.ts": data["global.d.ts"],
111
- "scripting.d.ts": data["scripting.d.ts"],
111
+ [const_1.globalDtsFileName]: data[const_1.globalDtsFileName],
112
+ [const_1.scriptingDtsFileName]: data[const_1.scriptingDtsFileName],
112
113
  });
113
114
  this.scriptName = data.scriptName;
114
115
  const scriptDir = (0, util_1.getScriptPath)(data.scriptName);
@@ -118,7 +119,7 @@ class Controller {
118
119
  (0, util_1.ensureDirectoryExistence)(filePath);
119
120
  await fs_1.default.promises.writeFile(filePath, content);
120
121
  }));
121
- console.log(chalk_1.default.green('global.d.ts and scripting.d.ts and other script files saved.'));
122
+ console.log(chalk_1.default.green(`${const_1.globalDtsFileName} and ${const_1.scriptingDtsFileName} and other script files saved.`));
122
123
  this.createWatcher(data.scriptName);
123
124
  ack({
124
125
  success: true,
@@ -154,8 +155,8 @@ class Controller {
154
155
  this.watcher = null;
155
156
  }
156
157
  await (0, util_1.writeDtsFiles)({
157
- "global.d.ts": data["global.d.ts"],
158
- "scripting.d.ts": data["scripting.d.ts"],
158
+ globalDtsFileName: data[const_1.globalDtsFileName],
159
+ scriptingDtsFileName: data[const_1.scriptingDtsFileName],
159
160
  });
160
161
  const scriptDir = (0, util_1.getScriptPath)(data.scriptName);
161
162
  if (!fs_1.default.existsSync(scriptDir)) {
@@ -168,6 +169,10 @@ class Controller {
168
169
  const readDir = async (dir) => {
169
170
  const files = await fs_1.default.promises.readdir(dir, { withFileTypes: true });
170
171
  await Promise.all(files.map(async (file) => {
172
+ // ignore .git and .vscode directories
173
+ if (file.name.startsWith('.git') || file.name.startsWith('.vscode')) {
174
+ return;
175
+ }
171
176
  if (file.isFile()) {
172
177
  const filePath = path_1.default.join(dir, file.name);
173
178
  const relativePath = (0, util_1.getRelativePath)(scriptDir, filePath);
package/dist/index.js CHANGED
@@ -1,5 +1,38 @@
1
1
  #!/usr/bin/env node
2
2
  "use strict";
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ o[k2] = m[k];
13
+ }));
14
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
16
+ }) : function(o, v) {
17
+ o["default"] = v;
18
+ });
19
+ var __importStar = (this && this.__importStar) || (function () {
20
+ var ownKeys = function(o) {
21
+ ownKeys = Object.getOwnPropertyNames || function (o) {
22
+ var ar = [];
23
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
24
+ return ar;
25
+ };
26
+ return ownKeys(o);
27
+ };
28
+ return function (mod) {
29
+ if (mod && mod.__esModule) return mod;
30
+ var result = {};
31
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
32
+ __setModuleDefault(result, mod);
33
+ return result;
34
+ };
35
+ })();
3
36
  var __importDefault = (this && this.__importDefault) || function (mod) {
4
37
  return (mod && mod.__esModule) ? mod : { "default": mod };
5
38
  };
@@ -55,6 +88,17 @@ const start_server_1 = require("./start_server");
55
88
  noAutoOpen: argv['no-auto-open'],
56
89
  startBonjourService: argv.bonjour,
57
90
  });
91
+ })
92
+ .command('migrate', 'Migrate the scripts in the workspace root directory to the scripts directory.', (yargs) => {
93
+ return yargs
94
+ .usage("$0 migrate")
95
+ .example([
96
+ ["$0 migrate", "Migrate the scripts in the workspace root directory to the scripts directory."]
97
+ ]);
98
+ }, async () => {
99
+ const { migrateOldFiles } = await Promise.resolve().then(() => __importStar(require('./util')));
100
+ await migrateOldFiles();
101
+ process.exit(0);
58
102
  })
59
103
  .showHelpOnFail(true)
60
104
  .option('help', {
@@ -30,7 +30,7 @@ function startServer({ port, noAutoOpen, startBonjourService }) {
30
30
  (0, util_1.createTsConfig)(); // Create tsconfig.json when the server starts
31
31
  (0, util_1.createVSCodeSettings)(); // Create vscode settings.json when the server starts
32
32
  (0, util_1.ensureScriptsDirectory)(); // Ensure the scripts directory exists
33
- await (0, util_1.migrateOldFiles)(); // Migrate old files to the scripts directory
33
+ // await migrateOldFiles(); // Migrate old files to the scripts directory
34
34
  const ipAddress = ip_1.default.address();
35
35
  const address = `http://${ipAddress}:${PORT}`;
36
36
  if (startBonjourService) {
@@ -44,7 +44,7 @@ function startServer({ port, noAutoOpen, startBonjourService }) {
44
44
  qrcode_terminal_1.default.generate(address, { small: true });
45
45
  if (startBonjourService) {
46
46
  function gracefulShutdown() {
47
- console.log('Shutting down Bonjour…');
47
+ console.log('Shutting down Bonjour...');
48
48
  service.stop(() => {
49
49
  bonjour.unpublishAll(() => {
50
50
  // wait for 500ms,let goodbye message be sent completely
package/dist/util.js CHANGED
@@ -20,6 +20,7 @@ const os_1 = __importDefault(require("os"));
20
20
  const child_process_1 = __importDefault(require("child_process"));
21
21
  const fs_1 = __importDefault(require("fs"));
22
22
  const chalk_1 = __importDefault(require("chalk"));
23
+ const const_1 = require("./const");
23
24
  function md5(content) {
24
25
  return crypto_1.default
25
26
  .createHash('md5')
@@ -71,12 +72,12 @@ function createTsConfig() {
71
72
  "jsxFragmentFactory": "Fragment",
72
73
  "paths": {
73
74
  "scripting": [
74
- "./dts/scripting.d.ts"
75
+ "./dts/${const_1.scriptingDtsFileName}"
75
76
  ]
76
77
  }
77
78
  },
78
79
  "include": [
79
- "./dts/global.d.ts",
80
+ "./dts/${const_1.globalDtsFileName}",
80
81
  "scripts"
81
82
  ]
82
83
  }`;
@@ -90,7 +91,7 @@ function createTsConfig() {
90
91
  const tsconfig = JSON.parse(fs_1.default.readFileSync(filePath, 'utf-8'));
91
92
  if (tsconfig.include && !tsconfig.include.includes('scripts')) {
92
93
  tsconfig.include = [
93
- "./dts/global.d.ts",
94
+ `./dts/${const_1.globalDtsFileName}`,
94
95
  "scripts"
95
96
  ];
96
97
  fs_1.default.writeFileSync(filePath, JSON.stringify(tsconfig, null, 2));
@@ -151,8 +152,9 @@ function getRelativePath(from, to) {
151
152
  async function migrateOldFiles() {
152
153
  try {
153
154
  const list = await fs_1.default.promises.readdir(process.cwd());
155
+ let hasMigrated = false;
154
156
  for (const file of list) {
155
- if (file === 'dts' || file === '.vscode' || file == "scripts" || file === 'tsconfig.json' || file === 'package.json' || file === 'node_modules') {
157
+ if (file === 'dts' || file == "scripts" || file === 'tsconfig.json' || file === 'package.json' || file === 'node_modules' || file.startsWith('.')) {
156
158
  continue;
157
159
  }
158
160
  const filePath = path_1.default.join(process.cwd(), file);
@@ -165,9 +167,16 @@ async function migrateOldFiles() {
165
167
  console.log(chalk_1.default.yellow(`File ${newFilePath} already exists, skipping...`));
166
168
  continue;
167
169
  }
170
+ hasMigrated = true;
168
171
  console.log(chalk_1.default.blue(`Moving ${filePath} to ${newFilePath}`));
169
172
  await fs_1.default.promises.rename(filePath, newFilePath);
170
173
  }
174
+ if (hasMigrated) {
175
+ console.log(chalk_1.default.green('Old files migrated to the scripts directory.'));
176
+ }
177
+ else {
178
+ console.log(chalk_1.default.gray('No old files to migrate.'));
179
+ }
171
180
  }
172
181
  catch (err) {
173
182
  console.log(`Failed to migrate old files: ${chalk_1.default.red(`${err}`)}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "scripting-cli",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
4
4
  "description": "This plugin integrates the desktop editor synchronization feature of the Scripting app.",
5
5
  "main": "dist/index.js",
6
6
  "bin": {
package/src/const.ts ADDED
@@ -0,0 +1,2 @@
1
+ export const globalDtsFileName = 'global.d.ts';
2
+ export const scriptingDtsFileName = 'scripting.d.ts';
package/src/controller.ts CHANGED
@@ -1,10 +1,11 @@
1
1
 
2
2
  import chokidar, { FSWatcher } from 'chokidar';
3
3
  import { Socket } from 'socket.io';
4
- import { ensureDirectoryExistence, getPath, getRelativePath, getScriptPath, md5, tryOpenFileInVSCode, writeDtsFiles } from './util';
4
+ import { ensureDirectoryExistence, getRelativePath, getScriptPath, md5, tryOpenFileInVSCode, writeDtsFiles } from './util';
5
5
  import fs from 'fs';
6
6
  import chalk from 'chalk';
7
7
  import path from 'path';
8
+ import { globalDtsFileName, scriptingDtsFileName } from './const';
8
9
 
9
10
  export class Controller {
10
11
 
@@ -55,7 +56,7 @@ export class Controller {
55
56
  fs.promises.readdir(getScriptPath()).then((files) => {
56
57
  const serverScriptNames = files.filter((filename) =>
57
58
  fs.statSync(getScriptPath(filename)).isDirectory()
58
- && filename !== '.vscode'
59
+ && !filename.startsWith('.')
59
60
  && filename !== 'dts'
60
61
  )
61
62
  socket.emit("serverScripts", serverScriptNames);
@@ -112,8 +113,8 @@ export class Controller {
112
113
  }
113
114
 
114
115
  handleSyncScriptFromClient = async (data: {
115
- 'global.d.ts': string,
116
- 'scripting.d.ts': string,
116
+ [globalDtsFileName]: string,
117
+ [scriptingDtsFileName]: string,
117
118
  scriptName: string
118
119
  scriptFiles: Record<string, string>
119
120
  }, ack: (result: {
@@ -136,8 +137,8 @@ export class Controller {
136
137
  }
137
138
 
138
139
  await writeDtsFiles({
139
- "global.d.ts": data["global.d.ts"],
140
- "scripting.d.ts": data["scripting.d.ts"],
140
+ [globalDtsFileName]: data[globalDtsFileName],
141
+ [scriptingDtsFileName]: data[scriptingDtsFileName],
141
142
  });
142
143
 
143
144
  this.scriptName = data.scriptName;
@@ -154,7 +155,7 @@ export class Controller {
154
155
  })
155
156
  );
156
157
 
157
- console.log(chalk.green('global.d.ts and scripting.d.ts and other script files saved.'));
158
+ console.log(chalk.green(`${globalDtsFileName} and ${scriptingDtsFileName} and other script files saved.`));
158
159
 
159
160
  this.createWatcher(data.scriptName);
160
161
 
@@ -207,8 +208,8 @@ export class Controller {
207
208
  }
208
209
 
209
210
  await writeDtsFiles({
210
- "global.d.ts": data["global.d.ts"],
211
- "scripting.d.ts": data["scripting.d.ts"],
211
+ globalDtsFileName: data[globalDtsFileName],
212
+ scriptingDtsFileName: data[scriptingDtsFileName],
212
213
  });
213
214
 
214
215
  const scriptDir = getScriptPath(data.scriptName);
@@ -229,6 +230,10 @@ export class Controller {
229
230
  const files = await fs.promises.readdir(dir, { withFileTypes: true });
230
231
  await Promise.all(
231
232
  files.map(async (file) => {
233
+ // ignore .git and .vscode directories
234
+ if (file.name.startsWith('.git') || file.name.startsWith('.vscode')) {
235
+ return;
236
+ }
232
237
  if (file.isFile()) {
233
238
  const filePath = path.join(dir, file.name);
234
239
  const relativePath = getRelativePath(scriptDir, filePath);
package/src/index.ts CHANGED
@@ -54,6 +54,17 @@ yargs(hideBin(process.argv))
54
54
  startBonjourService: argv.bonjour,
55
55
  });
56
56
  })
57
+ .command('migrate', 'Migrate the scripts in the workspace root directory to the scripts directory.', (yargs) => {
58
+ return yargs
59
+ .usage("$0 migrate")
60
+ .example([
61
+ ["$0 migrate", "Migrate the scripts in the workspace root directory to the scripts directory."]
62
+ ]);
63
+ }, async () => {
64
+ const { migrateOldFiles } = await import('./util');
65
+ await migrateOldFiles();
66
+ process.exit(0);
67
+ })
57
68
  .showHelpOnFail(true)
58
69
  .option('help', {
59
70
  alias: 'h',
@@ -6,7 +6,7 @@ import ip from 'ip';
6
6
  import Bonjour from 'bonjour';
7
7
  import chalk from 'chalk';
8
8
  import qrcode from 'qrcode-terminal';
9
- import { createTsConfig, createVSCodeSettings, ensureScriptsDirectory, migrateOldFiles, } from './util';
9
+ import { createTsConfig, createVSCodeSettings, ensureScriptsDirectory, } from './util';
10
10
  import { Controller } from './controller';
11
11
  import { initHttpRouter } from './http_router';
12
12
 
@@ -36,7 +36,8 @@ export function startServer({ port, noAutoOpen, startBonjourService }: {
36
36
  createTsConfig(); // Create tsconfig.json when the server starts
37
37
  createVSCodeSettings(); // Create vscode settings.json when the server starts
38
38
  ensureScriptsDirectory(); // Ensure the scripts directory exists
39
- await migrateOldFiles(); // Migrate old files to the scripts directory
39
+
40
+ // await migrateOldFiles(); // Migrate old files to the scripts directory
40
41
 
41
42
  const ipAddress = ip.address()
42
43
  const address = `http://${ipAddress}:${PORT}`;
@@ -55,7 +56,7 @@ export function startServer({ port, noAutoOpen, startBonjourService }: {
55
56
  if (startBonjourService) {
56
57
 
57
58
  function gracefulShutdown() {
58
- console.log('Shutting down Bonjour…');
59
+ console.log('Shutting down Bonjour...');
59
60
  service.stop(() => {
60
61
  bonjour.unpublishAll(() => {
61
62
  // wait for 500ms,let goodbye message be sent completely
package/src/util.ts CHANGED
@@ -4,6 +4,7 @@ import os from 'os';
4
4
  import childProcess from 'child_process';
5
5
  import fs from 'fs';
6
6
  import chalk from 'chalk';
7
+ import { globalDtsFileName, scriptingDtsFileName } from './const';
7
8
 
8
9
  export function md5(content: string): string {
9
10
  return crypto
@@ -59,12 +60,12 @@ export function createTsConfig() {
59
60
  "jsxFragmentFactory": "Fragment",
60
61
  "paths": {
61
62
  "scripting": [
62
- "./dts/scripting.d.ts"
63
+ "./dts/${scriptingDtsFileName}"
63
64
  ]
64
65
  }
65
66
  },
66
67
  "include": [
67
- "./dts/global.d.ts",
68
+ "./dts/${globalDtsFileName}",
68
69
  "scripts"
69
70
  ]
70
71
  }`;
@@ -79,7 +80,7 @@ export function createTsConfig() {
79
80
  const tsconfig = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
80
81
  if (tsconfig.include && !tsconfig.include.includes('scripts')) {
81
82
  tsconfig.include = [
82
- "./dts/global.d.ts",
83
+ `./dts/${globalDtsFileName}`,
83
84
  "scripts"
84
85
  ];
85
86
  fs.writeFileSync(filePath, JSON.stringify(tsconfig, null, 2));
@@ -148,10 +149,11 @@ export function getRelativePath(from: string, to: string) {
148
149
  export async function migrateOldFiles() {
149
150
  try {
150
151
  const list = await fs.promises.readdir(process.cwd());
152
+ let hasMigrated = false;
151
153
 
152
154
  for (const file of list) {
153
155
 
154
- if (file === 'dts' || file === '.vscode' || file == "scripts" || file === 'tsconfig.json' || file === 'package.json' || file === 'node_modules') {
156
+ if (file === 'dts' || file == "scripts" || file === 'tsconfig.json' || file === 'package.json' || file === 'node_modules' || file.startsWith('.')) {
155
157
  continue;
156
158
  }
157
159
 
@@ -170,11 +172,18 @@ export async function migrateOldFiles() {
170
172
  continue;
171
173
  }
172
174
 
175
+ hasMigrated = true;
173
176
  console.log(chalk.blue(`Moving ${filePath} to ${newFilePath}`));
174
177
  await fs.promises.rename(filePath, newFilePath);
175
178
 
176
179
  }
177
180
 
181
+ if (hasMigrated) {
182
+ console.log(chalk.green('Old files migrated to the scripts directory.'));
183
+ } else {
184
+ console.log(chalk.gray('No old files to migrate.'));
185
+ }
186
+
178
187
  } catch (err) {
179
188
  console.log(`Failed to migrate old files: ${chalk.red(`${err}`)}`);
180
189
  }