提交 6d4a9752 编写于 作者: fxy060608's avatar fxy060608

wip(uts): cli

上级 4980fea9
......@@ -7,6 +7,7 @@ packages/uni-mp-vite/dist
packages/uni-mp-compiler/dist
packages/uni-nvue-styler/dist
packages/vite-plugin-uni/dist
packages/uts/dist
.DS_Store
node_modules
......
#!/usr/bin/env node
/* eslint-disable no-restricted-globals */
require('@dcloudio/uts/dist/cli/index.js')
......@@ -2,7 +2,11 @@
"name": "@dcloudio/uni-app-plus",
"version": "3.0.0-alpha-3040820220424001",
"description": "@dcloudio/uni-app-plus",
"bin": {
"uts": "bin/uts.js"
},
"files": [
"bin",
"dist",
"lib",
"style"
......@@ -40,6 +44,7 @@
},
"dependencies": {
"@dcloudio/uni-app-vite": "3.0.0-alpha-3040820220424001",
"@dcloudio/uni-app-vue": "3.0.0-alpha-3040820220424001"
"@dcloudio/uni-app-vue": "3.0.0-alpha-3040820220424001",
"@dcloudio/uts": "3.0.0-alpha-3040820220424001"
}
}
#!/usr/bin/env node
require('../dist/uts/cli.js')
......@@ -2,9 +2,6 @@
"name": "@dcloudio/uni-app-vite",
"version": "3.0.0-alpha-3040820220424001",
"description": "uni-app-vite",
"bin": {
"uts": "bin/uts.js"
},
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
......@@ -32,10 +29,7 @@
"@vitejs/plugin-vue": "^2.3.2",
"@vue/compiler-dom": "3.2.33",
"@vue/compiler-sfc": "3.2.33",
"cac": "6.7.9",
"chokidar": "^3.5.3",
"debug": "^4.3.3",
"fast-glob": "^3.2.11",
"fs-extra": "^10.0.0",
"picocolors": "^1.0.0",
"rollup": "^2.59.0"
......@@ -46,8 +40,5 @@
"@vue/compiler-core": "3.2.33",
"esbuild": "^0.14.27",
"postcss": "^8.4.13"
},
"optionalDependencies": {
"@dcloudio/uts-darwin-arm64": "3.0.0-alpha-3040820220424001"
}
}
import fs from 'fs'
import path from 'path'
import glob from 'fast-glob'
// import { toKotlin } from '@dcloudio/uts'
import { normalizePath } from '@dcloudio/uni-cli-shared'
interface ToOptions {
watch?: boolean
input: {
dir: string
}
output: {
dir: string
sourceMap?: boolean
}
}
interface ToKotlinOptions extends ToOptions {}
function parseKotlinOptions(opts: Partial<ToKotlinOptions>) {
const { input, output } = opts
if (!input?.dir) {
throw new Error(`input.dir is required`)
}
if (!output?.dir) {
throw new Error(`output.dir is required`)
}
if (!fs.existsSync(input.dir)) {
throw new Error(`${input} is not found`)
}
return opts as ToKotlinOptions
}
function watchKotlin(opts: ToKotlinOptions) {}
function buildKotlin({ input: { dir: inputDir } }: ToKotlinOptions) {
const files = glob.sync(normalizePath(path.join(inputDir, '**/*.uts')))
console.log(files)
}
export function toKotlin(opts: ToKotlinOptions) {
opts = parseKotlinOptions(opts)
return opts.watch ? watchKotlin(opts) : buildKotlin(opts)
}
......@@ -41,4 +41,5 @@ export interface UtsSwiftOptions {
output: OutputSwiftOptions;
}
export interface UtsResult {
filename?: string;
}
......@@ -11,5 +11,13 @@
"type": "git",
"url": "git+https://github.com/dcloudio/uni-app.git",
"directory": "packages/uts"
},
"dependencies": {
"cac": "6.7.9",
"chokidar": "^3.5.3",
"fast-glob": "^3.2.11"
},
"optionalDependencies": {
"@dcloudio/uts-darwin-arm64": "3.0.0-alpha-3040820220424001"
}
}
import fs from 'fs'
import path from 'path'
import glob from 'fast-glob'
import { InputKotlinOptions, toKotlin } from '../index'
import { OutputKotlinOptions } from '../types'
export interface ToOptions {
watch?: boolean
input: {
dir: string
extname?: string
}
output: {
dir: string
sourceMap: boolean | string
}
}
interface ToKotlinOptions extends ToOptions {}
interface ToSwiftOptions extends ToOptions {}
function resolveDefaultOutputDir(inputDir: string) {
return path.resolve(inputDir, '../dist/kotlin')
}
function parseOptions(opts: Partial<ToKotlinOptions>) {
const { input } = opts
if (!input?.dir) {
throw new Error(`input.dir is required`)
}
if (!fs.existsSync(input.dir)) {
throw new Error(`${input} is not found`)
}
if (!opts.output) {
opts.output = {
dir: '',
sourceMap: false,
}
}
if (!opts.output.dir) {
opts.output.dir = resolveDefaultOutputDir(input.dir)
}
return opts as ToKotlinOptions
}
function watchSwift(_: ToSwiftOptions) {}
function buildSwift(_: ToSwiftOptions) {}
function watchKotlin(_: ToKotlinOptions) {}
function buildKotlin({
input: { dir: inputDir, extname },
output: { dir: outputDir, sourceMap },
}: ToKotlinOptions) {
const files = glob.sync('**/*' + (extname || '.uts'), {
absolute: true,
cwd: inputDir,
ignore: ['**/*.d' + (extname || '.uts')],
})
const input: InputKotlinOptions = {
root: inputDir,
filename: '',
}
const output: OutputKotlinOptions = {
outDir: outputDir,
sourceMap,
}
return Promise.all(
files.map((filename) => buildKotlinFile(filename, input, output))
)
}
function buildKotlinFile(
filename: string,
input: InputKotlinOptions,
output: OutputKotlinOptions
) {
const label = path.posix.relative(input.root, filename)
console.time(label)
return toKotlin({
input: {
...input,
filename,
namespace: '',
},
output: {
...output,
},
}).then((res) => {
console.timeEnd(label)
})
}
export function runDev(target: 'kotlin' | 'swift', opts: ToOptions) {
opts = parseOptions(opts)
switch (target) {
case 'kotlin':
return watchKotlin(opts)
case 'swift':
return watchSwift(opts)
}
}
export function runBuild(target: 'kotlin' | 'swift', opts: ToOptions) {
opts = parseOptions(opts)
switch (target) {
case 'kotlin':
return buildKotlin(opts)
case 'swift':
return buildSwift(opts)
}
}
import { cac } from 'cac'
import { toKotlin } from '.'
import { runBuild, runDev, ToOptions } from './action'
const cli = cac('uts')
......@@ -8,6 +8,7 @@ export interface CliOptions {
output: string
sourcemap: boolean
watch: boolean
extname: string
}
cli
......@@ -22,20 +23,24 @@ cli
.option('-w, --watch', `[boolean] rebuilds when uts have changed on disk`, {
default: false,
})
.option('-e, --extname [extname]', `[string] extname`, {
default: '.uts',
})
.action((root, opts: CliOptions) => {
console.log('opts', root, opts)
if (opts.target === 'kotlin') {
toKotlin({
watch: opts.watch,
input: {
dir: root,
},
output: {
dir: opts.output,
sourceMap: opts.sourcemap,
},
})
const toOptions: ToOptions = {
watch: opts.watch,
input: {
dir: root,
extname: opts.extname,
},
output: {
dir: opts.output,
sourceMap: opts.sourcemap,
},
}
return opts.watch
? runDev(opts.target, toOptions)
: runBuild(opts.target, toOptions)
})
cli.help()
......
......@@ -57,4 +57,6 @@ export interface UtsSwiftOptions {
output: OutputSwiftOptions
}
export interface UtsResult {}
export interface UtsResult {
filename?: string
}
......@@ -183,6 +183,7 @@ importers:
'@dcloudio/uni-h5': 3.0.0-alpha-3040820220424001
'@dcloudio/uni-i18n': 3.0.0-alpha-3040820220424001
'@dcloudio/uni-shared': 3.0.0-alpha-3040820220424001
'@dcloudio/uts': 3.0.0-alpha-3040820220424001
'@types/pako': 1.0.2
'@vue/compiler-sfc': 3.2.33
pako: ^1.0.11
......@@ -190,6 +191,7 @@ importers:
dependencies:
'@dcloudio/uni-app-vite': link:../uni-app-vite
'@dcloudio/uni-app-vue': link:../uni-app-vue
'@dcloudio/uts': link:../uts
devDependencies:
'@dcloudio/uni-cli-shared': link:../uni-cli-shared
'@dcloudio/uni-components': link:../uni-components
......@@ -208,7 +210,6 @@ importers:
'@dcloudio/uni-nvue-styler': 3.0.0-alpha-3040820220424001
'@dcloudio/uni-shared': 3.0.0-alpha-3040820220424001
'@dcloudio/uts': 3.0.0-alpha-3040820220424001
'@dcloudio/uts-darwin-arm64': 3.0.0-alpha-3040820220424001
'@rollup/pluginutils': ^4.2.0
'@types/debug': ^4.1.7
'@types/fs-extra': ^9.0.13
......@@ -216,11 +217,8 @@ importers:
'@vue/compiler-core': 3.2.33
'@vue/compiler-dom': 3.2.33
'@vue/compiler-sfc': 3.2.33
cac: 6.7.9
chokidar: ^3.5.3
debug: ^4.3.3
esbuild: ^0.14.27
fast-glob: ^3.2.11
fs-extra: ^10.0.0
picocolors: ^1.0.0
postcss: ^8.4.13
......@@ -235,15 +233,10 @@ importers:
'@vitejs/plugin-vue': 2.3.2
'@vue/compiler-dom': 3.2.33
'@vue/compiler-sfc': 3.2.33
cac: 6.7.9
chokidar: 3.5.3
debug: 4.3.4
fast-glob: 3.2.11
fs-extra: 10.1.0
picocolors: 1.0.0
rollup: 2.72.0
optionalDependencies:
'@dcloudio/uts-darwin-arm64': link:../uts-darwin-arm64
devDependencies:
'@types/debug': 4.1.7
'@types/fs-extra': 9.0.13
......@@ -775,7 +768,17 @@ importers:
'@dcloudio/uni-shared': link:../uni-shared
packages/uts:
specifiers: {}
specifiers:
'@dcloudio/uts-darwin-arm64': 3.0.0-alpha-3040820220424001
cac: 6.7.9
chokidar: ^3.5.3
fast-glob: ^3.2.11
dependencies:
cac: 6.7.9
chokidar: 3.5.3
fast-glob: 3.2.11
optionalDependencies:
'@dcloudio/uts-darwin-arm64': link:../uts-darwin-arm64
packages/uts-darwin-arm64:
specifiers: {}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册