scripting-cli 1.0.5 → 1.0.6

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.
@@ -31,7 +31,7 @@ export default class AppJsonPlugin implements WebpackPluginInstance {
31
31
  },
32
32
  (assets) => {
33
33
 
34
- const appJson: {
34
+ const scriptJson: {
35
35
  name: string
36
36
  index: string
37
37
  hash: string
@@ -43,19 +43,21 @@ export default class AppJsonPlugin implements WebpackPluginInstance {
43
43
  assets: Object.keys(assets),
44
44
  }
45
45
 
46
- const indexJs = appJson.assets.find(filePath => filePath.match(/\.js$/i) != null)
46
+ const indexJs = scriptJson.assets.find(
47
+ filePath => filePath.match(/\.js$/i) != null
48
+ )
47
49
 
48
50
  if (indexJs != null) {
49
51
  const [appName, ...others] = indexJs.split('/')
50
52
  const filePath = others[others.length - 1]
51
- appJson.name = appName
52
- appJson.index = filePath
53
- appJson.hash = filePath.replace('.js', '')
53
+ scriptJson.name = appName
54
+ scriptJson.index = filePath
55
+ scriptJson.hash = filePath.replace('.js', '')
54
56
 
55
57
  const indexJsSource = assets[indexJs]
56
58
 
57
- appJson.assets = [
58
- ...appJson.assets.filter(e => e !== indexJs),
59
+ scriptJson.assets = [
60
+ ...scriptJson.assets.filter(e => e !== indexJs),
59
61
  filePath,
60
62
  ]
61
63
 
@@ -65,8 +67,8 @@ export default class AppJsonPlugin implements WebpackPluginInstance {
65
67
  }
66
68
 
67
69
  compilation.emitAsset(
68
- 'app.json',
69
- new RawSource(JSON.stringify(appJson, null, 2))
70
+ 'script.json',
71
+ new RawSource(JSON.stringify(scriptJson, null, 2))
70
72
  )
71
73
 
72
74
  // const appMap: {
@@ -107,7 +109,7 @@ export default class AppJsonPlugin implements WebpackPluginInstance {
107
109
 
108
110
  // Object.entries(appMap).forEach(([appName, content]) => {
109
111
  // compilation.emitAsset(
110
- // appName + '/app.json',
112
+ // appName + '/script.json',
111
113
  // new RawSource(JSON.stringify(content, null, 2))
112
114
  // )
113
115
  // })
@@ -100,6 +100,10 @@ export default function getConfig() {
100
100
  },
101
101
  plugins: [
102
102
  new AppJsonPlugin(),
103
+ new webpack.BannerPlugin({
104
+ banner: 'const Scripting = require("scripting");',
105
+ raw: true
106
+ }),
103
107
  ],
104
108
  devtool: false,
105
109
  externals: {
@@ -0,0 +1,256 @@
1
+ import chalk from 'chalk'
2
+ import path from 'node:path'
3
+ import { execSync } from 'node:child_process'
4
+ import fs from 'node:fs'
5
+ import { mkdir, writeFile, copyFile } from 'node:fs/promises'
6
+
7
+ function getScriptDir(scriptName: string) {
8
+ return path.join(process.cwd(), scriptName)
9
+ }
10
+
11
+ async function createScriptDir(scriptName: string) {
12
+ const appDir = getScriptDir(scriptName)
13
+
14
+ if (fs.existsSync(appDir)) {
15
+ throw new Error(`Directory "./${scriptName}" already exists.`)
16
+ }
17
+
18
+ // const dir = path.resolve(appDir, 'src/svgs')
19
+ const srcDir = path.resolve(appDir, 'src')
20
+ const typesDir = path.resolve(appDir, 'types')
21
+ await Promise.all([
22
+ mkdir(srcDir, { recursive: true }),
23
+ mkdir(typesDir, { recursive: true })
24
+ ])
25
+ }
26
+
27
+ async function createEntryFile(scriptName: string) {
28
+ console.log(` > Creating ${scriptName}/src/index.tsx ...`)
29
+
30
+ const filePath = path.join(getScriptDir(scriptName), '/src/index.tsx')
31
+
32
+ await writeFile(
33
+ filePath,
34
+ `import { Column, CupertinoNavigationBar, CupertinoPageScaffold, Text, Container, useCupertinoColors, navigator, GestureDetector, Script, Center, CupertinoButton, } from 'scripting'
35
+
36
+ function App() {
37
+ const cupertinoColors = useCupertinoColors()
38
+ const backgroundColor = cupertinoColors.systemBackground
39
+
40
+ return (
41
+ <CupertinoPageScaffold
42
+ navigationBar={
43
+ <CupertinoNavigationBar
44
+ middle={
45
+ <Text>My Script</Text>
46
+ }
47
+ backgroundColor={backgroundColor}
48
+ />
49
+ }
50
+ >
51
+ <Center>
52
+ <Text
53
+ color={cupertinoColors.label}
54
+ >Welcome to Scripting!</Text>
55
+ </Center>
56
+ </CupertinoPageScaffold>
57
+ )
58
+ }
59
+
60
+ async function run() {
61
+ const result = await navigator.push({
62
+ element: <App />
63
+ })
64
+
65
+ Script.exit(result)
66
+ }
67
+
68
+ run()
69
+ `,
70
+ 'utf-8'
71
+ )
72
+
73
+ console.log(
74
+ chalk.green(` ✔️ Created ${scriptName}/src/index.tsx`)
75
+ )
76
+ }
77
+
78
+ async function createPackageJson(scriptName: string) {
79
+ console.log(` > Creating ${scriptName}/package.json ...`)
80
+
81
+ const filePath = path.join(getScriptDir(scriptName), 'package.json')
82
+
83
+ await writeFile(
84
+ filePath,
85
+ `{
86
+ "name": "${scriptName}",
87
+ "version": "0.0.1",
88
+ "private": true,
89
+ "scripts": {
90
+ "dev": "npx scripting-cli dev",
91
+ "build": "npx scripting-cli build"
92
+ }
93
+ }`,
94
+ 'utf-8'
95
+ )
96
+
97
+ console.log(
98
+ chalk.green(` ✔️ Created ${scriptName}/package.json`)
99
+ )
100
+ }
101
+
102
+ async function createReadme(scriptName: string) {
103
+
104
+ console.log(` > Creating ${scriptName}/README.md ...`)
105
+
106
+ const filePath = path.join(getScriptDir(scriptName), 'README.md')
107
+
108
+ await writeFile(
109
+ filePath,
110
+ `# ${scriptName} - A Scripting script
111
+
112
+ ## Start dev server
113
+
114
+ \`npm run dev\`
115
+
116
+ ## Build App
117
+
118
+ \`npm run build\`
119
+ `,
120
+ 'utf-8'
121
+ )
122
+
123
+ console.log(
124
+ chalk.green(` ✔️ Created ${scriptName}/README.md`)
125
+ )
126
+ }
127
+
128
+ async function createTSConfig(scriptName: string) {
129
+
130
+ console.log(` > Creating ${scriptName}/tsconfig.json ...`)
131
+
132
+ const filePath = path.join(getScriptDir(scriptName), 'tsconfig.json')
133
+
134
+ await writeFile(
135
+ filePath,
136
+ `{
137
+ "compilerOptions": {
138
+ "target": "ESNext",
139
+ "lib": [
140
+ "esnext"
141
+ ],
142
+ "allowJs": true,
143
+ "strict": true,
144
+ "esModuleInterop": true,
145
+ "allowSyntheticDefaultImports": true,
146
+ "forceConsistentCasingInFileNames": true,
147
+ "noFallthroughCasesInSwitch": true,
148
+ "module": "CommonJS",
149
+ "skipLibCheck": true,
150
+ "jsx": "react",
151
+ "jsxFactory": "createElement",
152
+ "baseUrl": "src",
153
+ "typeRoots": ["./types"],
154
+ // "paths": {
155
+ // "@/*": [
156
+ // "src/*"
157
+ // ]
158
+ // }
159
+ },
160
+ "include": [
161
+ "src"
162
+ ],
163
+ }`,
164
+ 'utf-8'
165
+ )
166
+
167
+ console.log(
168
+ chalk.green(` ✔️ Created ${scriptName}/tsconfig.json`)
169
+ )
170
+ }
171
+
172
+ async function copyAsset(scriptName: string, fileName: string) {
173
+ console.log(` > Creating ${scriptName}/types/${fileName}.d.ts ...`)
174
+ await copyFile(
175
+ path.join(__dirname, `../public/${fileName}.d.ts`),
176
+ path.join(getScriptDir(scriptName), `types/${fileName}.d.ts`)
177
+ )
178
+ console.log(
179
+ chalk.green(` ✔️ Created ${scriptName}/types/${fileName}.d.ts`)
180
+ )
181
+ }
182
+
183
+ async function copyAssets(scriptName: string) {
184
+ await copyAsset(scriptName, 'scripting')
185
+ await copyAsset(scriptName, 'global')
186
+ await copyAsset(scriptName, 'assets')
187
+
188
+ // console.log(` > Creating ${scriptName}/src/svgs/logo.svg ...`)
189
+ // await copyFile(
190
+ // path.join(__dirname, '../public/logo.svg'),
191
+ // path.join(getScriptDir(scriptName), 'src/svgs/logo.svg')
192
+ // )
193
+ // console.log(
194
+ // chalk.green(` ✔️ Created ${scriptName}/src/svgs/logo.svg`)
195
+ // )
196
+ }
197
+
198
+ async function createVSCodeSettings(scriptName: string) {
199
+ const destDir = path.join(getScriptDir(scriptName), '.vscode')
200
+
201
+ if (!fs.existsSync(destDir)) {
202
+ await mkdir(destDir, { recursive: true })
203
+ }
204
+
205
+ await copyFile(
206
+ path.join(__dirname, '../.vscode/settings.json'),
207
+ path.join(destDir, 'settings.json')
208
+ )
209
+ }
210
+
211
+ function installDeps(scriptName: string) {
212
+ console.log(
213
+ ` > cd ${scriptName} && npm i -D ts-loader typescript file-loader`
214
+ )
215
+ execSync(`cd ${scriptName} && npm i -D ts-loader typescript file-loader`)
216
+
217
+ console.log(
218
+ chalk.green(` ✔️ Scripting script ${chalk.bold(scriptName)} is ready!`)
219
+ )
220
+ }
221
+
222
+ export async function createScript(scriptName: string) {
223
+ if (!/^[a-zA-Z]+[a-zA-Z0-9-_]+$/.test(scriptName)) {
224
+ console.log(
225
+ chalk.red(`✖️ Invalid Scripting script name "${scriptName}"`)
226
+ )
227
+ return
228
+ }
229
+
230
+ console.log(
231
+ `Start creating ${scriptName} ...`
232
+ )
233
+
234
+ try {
235
+ await createScriptDir(scriptName)
236
+ await createPackageJson(scriptName)
237
+ await createTSConfig(scriptName)
238
+ await createReadme(scriptName)
239
+ await createEntryFile(scriptName)
240
+ await copyAssets(scriptName)
241
+ await createVSCodeSettings(scriptName)
242
+
243
+ installDeps(scriptName)
244
+
245
+ console.log(
246
+ `All done! Run ${chalk.bold(
247
+ chalk.green(`cd ${scriptName} && npm run dev`)
248
+ )} to start the dev server.`
249
+ )
250
+
251
+ } catch (e: any) {
252
+ console.log(
253
+ chalk.red(`✖️ Create script failed: ${e.message}`)
254
+ )
255
+ }
256
+ }
package/src/index.ts CHANGED
@@ -2,19 +2,20 @@
2
2
 
3
3
  import minimist from "minimist"
4
4
  import { startDevServer } from './startDevServer'
5
- import { buildApp } from './build'
6
- import { createApp } from './createApp'
5
+ // import { buildApp } from './build'
6
+ import { createScript } from './createScript'
7
7
 
8
8
  function run() {
9
9
  const args = minimist(process.argv.slice(2))
10
10
 
11
11
  if (args['create'] != null) {
12
- createApp(args['create'])
12
+ createScript(args['create'])
13
13
  } else if (args._.includes('dev')) {
14
14
  startDevServer()
15
- } else if (args._.includes('build')) {
16
- buildApp()
17
15
  }
16
+ // else if (args._.includes('build')) {
17
+ // buildApp()
18
+ // }
18
19
 
19
20
  }
20
21
 
@@ -37,28 +37,23 @@ export function startDevServer() {
37
37
  'Start running webpack compiler in watch mode...'
38
38
  )
39
39
 
40
- compiler.watch(
41
- {
40
+ compiler.watch({}, (err, stats) => {
41
+ utils.clearConsole()
42
+ if (err) {
43
+ console.log(
44
+ chalk.red(' ✖️ webpack compile error: \n') + err
45
+ )
46
+ } else {
47
+ console.log(stats?.toString({
48
+ chunks: false,
49
+ colors: true,
50
+ }))
42
51
 
43
- },
44
- (err, stats) => {
45
- utils.clearConsole()
46
- if (err) {
47
- console.log(
48
- chalk.red(' ✖️ webpack compile error: \n') + err
49
- )
50
- } else {
51
- console.log(stats?.toString({
52
- chunks: false,
53
- colors: true,
54
- }))
55
-
56
- if (stats?.toJson().errorsCount === 0) {
57
- onCompileDone()
58
- }
52
+ if (stats?.toJson().errorsCount === 0) {
53
+ onCompileDone()
59
54
  }
60
55
  }
61
- )
56
+ })
62
57
 
63
58
  async function onCompileDone() {
64
59
  if (isStarted) {
@@ -68,16 +63,16 @@ export function startDevServer() {
68
63
  chalk.gray(` ~ Sent update event to ${connectedSockets.length} client(s)`)
69
64
  )
70
65
 
71
- const appJson = await readFile(
66
+ const scriptJson = await readFile(
72
67
  path.resolve(
73
68
  process.cwd(),
74
- 'build/app.json'
69
+ 'build/script.json'
75
70
  ),
76
71
  'utf-8'
77
72
  )
78
73
 
79
74
  for (const socket of connectedSockets) {
80
- socket.emit('update', appJson)
75
+ socket.emit('update', scriptJson)
81
76
  }
82
77
  } else {
83
78
  start()
package/dist/createApp.js DELETED
@@ -1,223 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.createApp = void 0;
16
- const chalk_1 = __importDefault(require("chalk"));
17
- const node_path_1 = __importDefault(require("node:path"));
18
- const node_child_process_1 = require("node:child_process");
19
- const node_fs_1 = __importDefault(require("node:fs"));
20
- const promises_1 = require("node:fs/promises");
21
- function getAppDir(appName) {
22
- return node_path_1.default.join(process.cwd(), appName);
23
- }
24
- function createAppDir(appName) {
25
- return __awaiter(this, void 0, void 0, function* () {
26
- const appDir = getAppDir(appName);
27
- if (node_fs_1.default.existsSync(appDir)) {
28
- throw new Error(`Directory "./${appName}" already exists.`);
29
- }
30
- const dir = node_path_1.default.resolve(appDir, 'src/svgs');
31
- yield (0, promises_1.mkdir)(dir, { recursive: true });
32
- });
33
- }
34
- function createEntryFile(appName) {
35
- return __awaiter(this, void 0, void 0, function* () {
36
- console.log(` > Creating ${appName}/src/index.tsx ...`);
37
- const filePath = node_path_1.default.join(getAppDir(appName), '/src/index.tsx');
38
- yield (0, promises_1.writeFile)(filePath, `import { runApp, createElement, RouterProvider, Router, Column, CupertinoNavigationBar, CupertinoPageScaffold, Svg, Text, Container, useCupertinoColors } from 'scripting'
39
- import Logo from './svgs/logo.svg'
40
-
41
- function ScriptingLogo() {
42
- return (
43
- <Container
44
- clipBehavior={'hardEdge'}
45
- width={80}
46
- height={80}
47
- margin={{
48
- vertical: 100,
49
- }}
50
- borderRadius={16}
51
- >
52
- <Svg src={Logo} />
53
- </Container>
54
- )
55
- }
56
-
57
- function HomePage() {
58
- const cupertinoColors = useCupertinoColors()
59
- const backgroundColor = cupertinoColors.systemBackground
60
-
61
- return (
62
- <CupertinoPageScaffold
63
- navigationBar={
64
- <CupertinoNavigationBar
65
- middle={
66
- <Text>${appName}</Text>
67
- }
68
- backgroundColor={backgroundColor}
69
- />
70
- }
71
- >
72
- <Container
73
- width={'infinity'}
74
- height={'infinity'}
75
- color={backgroundColor}
76
- >
77
- <Column
78
- crossAxisAlignment={'center'}
79
- >
80
- <ScriptingLogo />
81
- <Text
82
- color={cupertinoColors.label}
83
- >Welcome to Scripting!</Text>
84
- </Column>
85
- </Container>
86
- </CupertinoPageScaffold>
87
- )
88
- }
89
-
90
- const router = new Router([
91
- {
92
- path: '/',
93
- element: <HomePage />,
94
- },
95
- ])
96
-
97
- runApp(
98
- <RouterProvider router={router} />
99
- )`, 'utf-8');
100
- console.log(chalk_1.default.green(` ✔️ Created ${appName}/src/index.tsx`));
101
- });
102
- }
103
- function createPackageJson(appName) {
104
- return __awaiter(this, void 0, void 0, function* () {
105
- console.log(` > Creating ${appName}/package.json ...`);
106
- const filePath = node_path_1.default.join(getAppDir(appName), 'package.json');
107
- yield (0, promises_1.writeFile)(filePath, `{
108
- "name": "${appName}",
109
- "version": "0.0.1",
110
- "private": true,
111
- "scripts": {
112
- "dev": "npx scripting-cli dev",
113
- "build": "npx scripting-cli build"
114
- }
115
- }`, 'utf-8');
116
- console.log(chalk_1.default.green(` ✔️ Created ${appName}/package.json`));
117
- });
118
- }
119
- function createReadme(appName) {
120
- return __awaiter(this, void 0, void 0, function* () {
121
- console.log(` > Creating ${appName}/README.md ...`);
122
- const filePath = node_path_1.default.join(getAppDir(appName), 'README.md');
123
- yield (0, promises_1.writeFile)(filePath, `# ${appName} - A Scripting application
124
-
125
- ## Start dev server
126
-
127
- \`npm run dev\`
128
-
129
- ## Build App
130
-
131
- \`npm run build\`
132
- `, 'utf-8');
133
- console.log(chalk_1.default.green(` ✔️ Created ${appName}/README.md`));
134
- });
135
- }
136
- function createTSConfig(appName) {
137
- return __awaiter(this, void 0, void 0, function* () {
138
- console.log(` > Creating ${appName}/tsconfig.json ...`);
139
- const filePath = node_path_1.default.join(getAppDir(appName), 'tsconfig.json');
140
- yield (0, promises_1.writeFile)(filePath, `{
141
- "compilerOptions": {
142
- "target": "es5",
143
- "lib": [
144
- "esnext",
145
- ],
146
- "allowJs": false,
147
- "skipLibCheck": true,
148
- "esModuleInterop": true,
149
- "allowSyntheticDefaultImports": true,
150
- "strict": true,
151
- "forceConsistentCasingInFileNames": true,
152
- "noFallthroughCasesInSwitch": true,
153
- "module": "esnext",
154
- "moduleResolution": "node",
155
- "resolveJsonModule": true,
156
- "isolatedModules": true,
157
- "jsx": "react",
158
- "jsxFactory": "createElement",
159
- "baseUrl": "src",
160
- "paths": {
161
- "@/*": [
162
- "src/*"
163
- ]
164
- }
165
- },
166
- "include": [
167
- "src"
168
- ],
169
- }`, 'utf-8');
170
- console.log(chalk_1.default.green(` ✔️ Created ${appName}/tsconfig.json`));
171
- });
172
- }
173
- function copyAssets(appName) {
174
- return __awaiter(this, void 0, void 0, function* () {
175
- console.log(` > Creating ${appName}/src/scripting.d.ts ...`);
176
- yield (0, promises_1.copyFile)(node_path_1.default.join(__dirname, '../public/scripting.d.ts'), node_path_1.default.join(getAppDir(appName), 'src/scripting.d.ts'));
177
- console.log(chalk_1.default.green(` ✔️ Created ${appName}/src/scripting.d.ts`));
178
- console.log(` > Creating ${appName}/src/assets.d.ts ...`);
179
- yield (0, promises_1.copyFile)(node_path_1.default.join(__dirname, '../public/assets.d.ts'), node_path_1.default.join(getAppDir(appName), 'src/assets.d.ts'));
180
- console.log(chalk_1.default.green(` ✔️ Created ${appName}/src/assets.d.ts`));
181
- console.log(` > Creating ${appName}/src/svgs/logo.svg ...`);
182
- yield (0, promises_1.copyFile)(node_path_1.default.join(__dirname, '../public/logo.svg'), node_path_1.default.join(getAppDir(appName), 'src/svgs/logo.svg'));
183
- console.log(chalk_1.default.green(` ✔️ Created ${appName}/src/svgs/logo.svg`));
184
- });
185
- }
186
- function createVSCodeSettings(appName) {
187
- return __awaiter(this, void 0, void 0, function* () {
188
- const destDir = node_path_1.default.join(getAppDir(appName), '.vscode');
189
- if (!node_fs_1.default.existsSync(destDir)) {
190
- yield (0, promises_1.mkdir)(destDir, { recursive: true });
191
- }
192
- yield (0, promises_1.copyFile)(node_path_1.default.join(__dirname, '../.vscode/settings.json'), node_path_1.default.join(destDir, 'settings.json'));
193
- });
194
- }
195
- function installDeps(appName) {
196
- console.log(` > cd ${appName} && npm i -D ts-loader typescript file-loader`);
197
- (0, node_child_process_1.execSync)(`cd ${appName} && npm i -D ts-loader typescript file-loader`);
198
- console.log(chalk_1.default.green(` ✔️ Scripting application ${chalk_1.default.bold(appName)} is ready!`));
199
- }
200
- function createApp(appName) {
201
- return __awaiter(this, void 0, void 0, function* () {
202
- if (!/^[a-zA-Z]+[a-zA-Z0-9-_]+$/.test(appName)) {
203
- console.log(chalk_1.default.red(`✖️ Invalid Scripting application name "${appName}"`));
204
- return;
205
- }
206
- console.log(`Start creating ${appName} ...`);
207
- try {
208
- yield createAppDir(appName);
209
- yield createPackageJson(appName);
210
- yield createTSConfig(appName);
211
- yield createReadme(appName);
212
- yield createEntryFile(appName);
213
- yield copyAssets(appName);
214
- yield createVSCodeSettings(appName);
215
- installDeps(appName);
216
- console.log(`All done! Run ${chalk_1.default.bold(chalk_1.default.green(`cd ${appName} && npm run dev`))} to start the dev server.`);
217
- }
218
- catch (e) {
219
- console.log(chalk_1.default.red(`✖️ Create Scripting application failed: ${e.message}`));
220
- }
221
- });
222
- }
223
- exports.createApp = createApp;