scripting-cli 1.0.4 → 1.0.5

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/build.ts CHANGED
@@ -1,10 +1,26 @@
1
1
  import chalk from 'chalk'
2
2
  import Webpack from "webpack"
3
- import webpackConfig from './config/webpack.config'
4
3
  import { zip } from 'cross-zip'
4
+ import qrcode from 'qrcode-terminal'
5
+
5
6
  import path from 'node:path'
7
+ import http from 'node:http'
8
+ import express from 'express'
9
+
10
+ import getConfig from './config/webpack.config'
11
+ import * as utils from './utils'
12
+ import minimist from 'minimist'
6
13
 
7
14
  export function buildApp() {
15
+ const webpackConfig = getConfig()
16
+
17
+ const appName = Object.keys(webpackConfig.entry as any)[0]
18
+ const appFile = `${appName}.zip`
19
+
20
+ const outputDir = path.resolve(process.cwd(), appName)
21
+
22
+ webpackConfig.output!.path = outputDir
23
+
8
24
  const compiler = Webpack(webpackConfig)
9
25
 
10
26
  console.log(
@@ -32,28 +48,63 @@ export function buildApp() {
32
48
  }
33
49
  })
34
50
 
35
- }
36
51
 
37
- function createAppFile() {
38
- const appFile = `${Object.keys(webpackConfig.entry as any)[0]}.zip`
39
52
 
40
- console.log(
41
- ` ~ Creating ${appFile}....`
42
- )
53
+ function createAppFile() {
54
+ console.log(
55
+ ` ~ Creating ${appFile}....`
56
+ )
43
57
 
44
- zip(
45
- path.resolve(process.cwd(), './build'),
46
- path.resolve(process.cwd(), `./build/${appFile}`),
47
- (error) => {
48
- if (error != null) {
49
- console.log(
50
- chalk.red('Error zipping app files: \n') + error
51
- )
52
- } else {
53
- console.log(
54
- chalk.green(` ✔️ ${appFile} created.`)
55
- )
58
+ zip(
59
+ outputDir,
60
+ path.resolve(outputDir, appFile),
61
+ (error) => {
62
+ if (error != null) {
63
+ console.log(
64
+ chalk.red('Error zipping app files: \n') + error
65
+ )
66
+ } else {
67
+ console.log(
68
+ chalk.green(` ✔️ ${appFile} created.`)
69
+ )
70
+
71
+ startServer()
72
+ }
56
73
  }
57
- }
58
- )
74
+ )
75
+ }
76
+
77
+ function startServer() {
78
+ const app = express()
79
+ const server = http.createServer(app)
80
+
81
+ const args = minimist(process.argv.slice(2))
82
+ const ipAddress = utils.getIpAddress()
83
+ const port = args['port'] || 8000
84
+
85
+ app.all('*', function (req, res, next) {
86
+ res.header('Access-Control-Allow-Origin', '*')
87
+ res.header('Access-Control-Allow-Headers', '*')
88
+ res.header('Access-Control-Allow-Methods', 'GET, POST, PATCH, OPTIONS, PUT, DELETE')
89
+ next()
90
+ })
91
+
92
+ app.use(
93
+ '/',
94
+ express.static(
95
+ outputDir
96
+ )
97
+ )
98
+
99
+ server.listen(port)
100
+
101
+ console.log(`Server running at ${chalk.bold(
102
+ chalk.green(
103
+ `http://${ipAddress}:${port}`
104
+ ),
105
+ )}`)
106
+ console.log(`Use the Scripting app to scan the QR code below to install:`)
107
+ qrcode.generate(`http://${ipAddress}:${port}`, { small: true })
108
+ }
109
+
59
110
  }
@@ -8,101 +8,104 @@ import webpack from 'webpack'
8
8
 
9
9
  import AppJsonPlugin from './AppJsonPlugin'
10
10
 
11
- const baseDir = process.cwd()
12
- const srcDir = path.resolve(baseDir, 'src')
13
- const buildDir = path.resolve(baseDir, 'build')
11
+ export default function getConfig() {
14
12
 
15
- const pkgPath = path.resolve(baseDir, 'package.json')
13
+ const baseDir = process.cwd()
14
+ const srcDir = path.resolve(baseDir, 'src')
15
+ const outputDir = path.resolve(baseDir, 'build')
16
16
 
17
- if (!fs.existsSync(pkgPath)) {
18
- console.log(
19
- chalk.red('✖️ package.json not found.')
20
- )
21
- process.exit(404)
22
- }
17
+ const pkgPath = path.resolve(baseDir, 'package.json')
18
+
19
+ if (!fs.existsSync(pkgPath)) {
20
+ console.log(
21
+ chalk.red('✖️ package.json not found.')
22
+ )
23
+ process.exit(404)
24
+ }
23
25
 
24
- const pkg = JSON.parse(
25
- fs.readFileSync(
26
- pkgPath,
27
- 'utf8'
26
+ const pkg = JSON.parse(
27
+ fs.readFileSync(
28
+ pkgPath,
29
+ 'utf8'
30
+ )
28
31
  )
29
- )
30
32
 
31
- const entry = {
32
- [pkg.name]: path.join(srcDir, 'index.tsx'),
33
- }
33
+ const entry = {
34
+ [pkg.name]: path.join(srcDir, 'index.tsx'),
35
+ }
34
36
 
35
- const config: webpack.Configuration = {
36
- mode: 'production',
37
- entry,
38
- optimization: {
39
- moduleIds: 'deterministic',
40
- chunkIds: 'named',
41
- minimizer: [
42
- new TerserPlugin({
43
- extractComments: false,
44
- terserOptions: {
45
- format: {
46
- comments: false,
37
+ const config: webpack.Configuration = {
38
+ mode: 'production',
39
+ entry,
40
+ optimization: {
41
+ moduleIds: 'deterministic',
42
+ chunkIds: 'named',
43
+ minimizer: [
44
+ new TerserPlugin({
45
+ extractComments: false,
46
+ terserOptions: {
47
+ format: {
48
+ comments: false,
49
+ },
47
50
  },
51
+ }),
52
+ ],
53
+ },
54
+ module: {
55
+ rules: [
56
+ {
57
+ test: /\.tsx?$/,
58
+ use: 'ts-loader',
59
+ exclude: /node_modules/,
48
60
  },
49
- }),
50
- ],
51
- },
52
- module: {
53
- rules: [
54
- {
55
- test: /\.tsx?$/,
56
- use: 'ts-loader',
57
- exclude: /node_modules/,
58
- },
59
- {
60
- test: /\.(png|jpe?g|gif|svg)$/i,
61
- loader: 'file-loader',
62
- options: {
63
- name: '[path][contenthash:8].[ext]',
64
- outputPath: (url: string, resourcePath: string, context: any) => {
65
- // if (url.startsWith('src/app/')) {
66
- // return url.replace('src/app/', '')
67
- // }
68
- return url.replace('src/', '')
69
- // return url.replace(/src\/([^/]+)\//, '')
70
- },
71
- publicPath: (url: string, resourcePath: string, context: any) => {
72
- // console.log([url, resourcePath])
73
- // return url.replace(/.*src\/app\/(\w+\/)?/, '')
74
- // if (url.startsWith('src/app/')) {
75
- // return url.replace('src/app/', '')
76
- // }
77
- return url.replace('src/', '')
61
+ {
62
+ test: /\.(png|jpe?g|gif|svg)$/i,
63
+ loader: 'file-loader',
64
+ options: {
65
+ name: '[path][contenthash:8].[ext]',
66
+ outputPath: (url: string, resourcePath: string, context: any) => {
67
+ // if (url.startsWith('src/app/')) {
68
+ // return url.replace('src/app/', '')
69
+ // }
70
+ return url.replace('src/', '')
71
+ // return url.replace(/src\/([^/]+)\//, '')
72
+ },
73
+ publicPath: (url: string, resourcePath: string, context: any) => {
74
+ // console.log([url, resourcePath])
75
+ // return url.replace(/.*src\/app\/(\w+\/)?/, '')
76
+ // if (url.startsWith('src/app/')) {
77
+ // return url.replace('src/app/', '')
78
+ // }
79
+ return url.replace('src/', '')
80
+ },
78
81
  },
79
82
  },
80
- },
81
- ],
82
- },
83
- resolve: {
84
- extensions: ['.tsx', '.ts', '.js'],
85
- modules: [
86
- 'node_modules',
87
- srcDir,
83
+ ],
84
+ },
85
+ resolve: {
86
+ extensions: ['.tsx', '.ts', '.js'],
87
+ modules: [
88
+ 'node_modules',
89
+ srcDir,
90
+ ],
91
+ alias: {
92
+ '@': srcDir,
93
+ }
94
+ },
95
+ output: {
96
+ filename: '[name]/[contenthash:8].js',
97
+ // filename: '[contenthash:8].js',
98
+ path: outputDir,
99
+ clean: true,
100
+ },
101
+ plugins: [
102
+ new AppJsonPlugin(),
88
103
  ],
89
- alias: {
90
- '@': srcDir,
104
+ devtool: false,
105
+ externals: {
106
+ 'scripting': 'Scripting',
91
107
  }
92
- },
93
- output: {
94
- filename: '[name]/[contenthash:8].js',
95
- // filename: '[contenthash:8].js',
96
- path: buildDir,
97
- clean: true,
98
- },
99
- plugins: [
100
- new AppJsonPlugin(),
101
- ],
102
- devtool: false,
103
- externals: {
104
- 'scripting': 'Scripting',
105
108
  }
106
- }
107
109
 
108
- export default config
110
+ return config
111
+ }
package/src/createApp.ts CHANGED
@@ -26,68 +26,68 @@ async function createEntryFile(appName: string) {
26
26
 
27
27
  await writeFile(
28
28
  filePath,
29
- `
30
-
31
- import { runApp, createElement, RouterProvider, Router, Column, CupertinoNavigationBar, CupertinoPageScaffold, Svg, Text, Container, Color } from 'scripting'
32
- import Logo from './svgs/logo.svg'
33
-
34
- function ScriptingLogo() {
35
- return (
36
- <Container
37
- clipBehavior={'hardEdge'}
38
- width={80}
39
- height={80}
40
- margin={{
41
- vertical: 100,
42
- }}
43
- borderRadius={16}
44
- >
45
- <Svg src={Logo} />
46
- </Container>
47
- )
48
- }
49
-
50
- function HomePage() {
51
- const backgroundColor: Color = '#ffffff'
52
- return (
53
- <CupertinoPageScaffold
54
- navigationBar={
55
- <CupertinoNavigationBar
56
- middle={
57
- <Text>${appName}</Text>
58
- }
59
- backgroundColor={backgroundColor}
60
- />
29
+ `import { runApp, createElement, RouterProvider, Router, Column, CupertinoNavigationBar, CupertinoPageScaffold, Svg, Text, Container, useCupertinoColors } from 'scripting'
30
+ import Logo from './svgs/logo.svg'
31
+
32
+ function ScriptingLogo() {
33
+ return (
34
+ <Container
35
+ clipBehavior={'hardEdge'}
36
+ width={80}
37
+ height={80}
38
+ margin={{
39
+ vertical: 100,
40
+ }}
41
+ borderRadius={16}
42
+ >
43
+ <Svg src={Logo} />
44
+ </Container>
45
+ )
46
+ }
47
+
48
+ function HomePage() {
49
+ const cupertinoColors = useCupertinoColors()
50
+ const backgroundColor = cupertinoColors.systemBackground
51
+
52
+ return (
53
+ <CupertinoPageScaffold
54
+ navigationBar={
55
+ <CupertinoNavigationBar
56
+ middle={
57
+ <Text>${appName}</Text>
61
58
  }
59
+ backgroundColor={backgroundColor}
60
+ />
61
+ }
62
+ >
63
+ <Container
64
+ width={'infinity'}
65
+ height={'infinity'}
66
+ color={backgroundColor}
67
+ >
68
+ <Column
69
+ crossAxisAlignment={'center'}
62
70
  >
63
- <Container
64
- width={'infinity'}
65
- height={'infinity'}
66
- color={backgroundColor}
67
- >
68
- <Column
69
- crossAxisAlignment={'center'}
70
- >
71
- <ScriptingLogo />
72
- <Text>Welcome to Scripting!</Text>
73
- </Column>
74
- </Container>
75
- </CupertinoPageScaffold>
76
- )
77
- }
78
-
79
- const router = new Router([
80
- {
81
- path: '/',
82
- element: <HomePage />,
83
- },
84
- ])
85
-
86
- runApp(
87
- <RouterProvider router={router} />
88
- )
89
-
90
- `,
71
+ <ScriptingLogo />
72
+ <Text
73
+ color={cupertinoColors.label}
74
+ >Welcome to Scripting!</Text>
75
+ </Column>
76
+ </Container>
77
+ </CupertinoPageScaffold>
78
+ )
79
+ }
80
+
81
+ const router = new Router([
82
+ {
83
+ path: '/',
84
+ element: <HomePage />,
85
+ },
86
+ ])
87
+
88
+ runApp(
89
+ <RouterProvider router={router} />
90
+ )`,
91
91
  'utf-8'
92
92
  )
93
93
 
@@ -222,23 +222,26 @@ async function copyAssets(appName: string) {
222
222
  }
223
223
 
224
224
  async function createVSCodeSettings(appName: string) {
225
- const destDir = path.join(process.cwd(), '.vscode')
225
+ const destDir = path.join(getAppDir(appName), '.vscode')
226
226
 
227
227
  if (!fs.existsSync(destDir)) {
228
- await mkdir(destDir)
228
+ await mkdir(destDir, { recursive: true })
229
229
  }
230
230
 
231
231
  await copyFile(
232
232
  path.join(__dirname, '../.vscode/settings.json'),
233
- path.join(getAppDir(appName), '.vscode/settings.json')
233
+ path.join(destDir, 'settings.json')
234
234
  )
235
235
  }
236
236
 
237
237
  function installDeps(appName: string) {
238
+ console.log(
239
+ ` > cd ${appName} && npm i -D ts-loader typescript file-loader`
240
+ )
238
241
  execSync(`cd ${appName} && npm i -D ts-loader typescript file-loader`)
239
242
 
240
243
  console.log(
241
- chalk.green(` ✔️ Scripting application ${chalk.bold(appName)} created!`)
244
+ chalk.green(` ✔️ Scripting application ${chalk.bold(appName)} is ready!`)
242
245
  )
243
246
  }
244
247
 
@@ -250,6 +253,10 @@ export async function createApp(appName: string) {
250
253
  return
251
254
  }
252
255
 
256
+ console.log(
257
+ `Start creating ${appName} ...`
258
+ )
259
+
253
260
  try {
254
261
  await createAppDir(appName)
255
262
  await createPackageJson(appName)
@@ -262,8 +269,8 @@ export async function createApp(appName: string) {
262
269
  installDeps(appName)
263
270
 
264
271
  console.log(
265
- ` ✔️ All done! Run ${chalk.bold(
266
- chalk.green('npm run dev')
272
+ `All done! Run ${chalk.bold(
273
+ chalk.green(`cd ${appName} && npm run dev`)
267
274
  )} to start the dev server.`
268
275
  )
269
276
 
package/src/index.ts CHANGED
@@ -8,7 +8,6 @@ import { createApp } from './createApp'
8
8
  function run() {
9
9
  const args = minimist(process.argv.slice(2))
10
10
 
11
- // console.log(args)
12
11
  if (args['create'] != null) {
13
12
  createApp(args['create'])
14
13
  } else if (args._.includes('dev')) {
@@ -11,7 +11,7 @@ import { Server, Socket } from 'socket.io'
11
11
  import * as utils from './utils'
12
12
 
13
13
  import Webpack from "webpack"
14
- import webpackConfig from './config/webpack.config'
14
+ import getConfig from './config/webpack.config'
15
15
 
16
16
  export function startDevServer() {
17
17
  const args = minimist(process.argv.slice(2))
@@ -23,9 +23,12 @@ export function startDevServer() {
23
23
  const server = http.createServer(app)
24
24
  const io = new Server(server)
25
25
 
26
+
26
27
  let isStarted = false
27
28
  let connectedSockets: Socket[] = []
28
29
 
30
+ const webpackConfig = getConfig()
31
+
29
32
  webpackConfig.mode = 'development'
30
33
 
31
34
  const compiler = Webpack(webpackConfig)