diff --git a/packages/uts-darwin-arm64/uts.darwin-arm64.node b/packages/uts-darwin-arm64/uts.darwin-arm64.node index e1a7c1ba707e35929ebddc04cc52d5e31681d552..5152294438941ce8a57a958464781eb129859e7e 100755 Binary files a/packages/uts-darwin-arm64/uts.darwin-arm64.node and b/packages/uts-darwin-arm64/uts.darwin-arm64.node differ diff --git a/packages/uts/dist/index.d.ts b/packages/uts/dist/index.d.ts index 5adafa8eb22c1fd4a9456a894dcc888747899195..b44df5af8ef867f5714405642de6ba4d378dc754 100644 --- a/packages/uts/dist/index.d.ts +++ b/packages/uts/dist/index.d.ts @@ -1,4 +1,14 @@ -import { UtsKotlinOptions, UtsResult, UtsSwiftOptions } from './types'; -export * from './types'; -export declare function toKotlin(options: UtsKotlinOptions): Promise; -export declare function toSwift(options: UtsSwiftOptions): Promise; +export interface ToOptions { + watch?: boolean; + input: { + dir: string; + extname?: string; + }; + output: { + dir: string; + sourceMap: boolean | string; + inlineSourcesContent?: boolean; + }; +} +export declare function runDev(target: 'kotlin' | 'swift', opts: ToOptions): void; +export declare function runBuild(target: 'kotlin' | 'swift', opts: ToOptions): void | Promise; diff --git a/packages/uts/dist/index.js b/packages/uts/dist/index.js index 9cc31c204290d3a1268393b576835b990257741b..7785474ce60537aeafb02015153972a3029ab2da 100644 --- a/packages/uts/dist/index.js +++ b/packages/uts/dist/index.js @@ -1,46 +1,112 @@ "use strict"; -var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - var desc = Object.getOwnPropertyDescriptor(m, k); - if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { - desc = { enumerable: true, get: function() { return m[k]; } }; - } - Object.defineProperty(o, k2, desc); -}) : (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - o[k2] = m[k]; -})); -var __exportStar = (this && this.__exportStar) || function(m, exports) { - for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); -exports.toSwift = exports.toKotlin = void 0; -const path_1 = require("path"); -__exportStar(require("./types"), exports); -// Allow overrides to the location of the .node binding file -const bindingsOverride = process.env['UTS_BINARY_PATH']; -const bindings = !!bindingsOverride - ? require((0, path_1.resolve)(bindingsOverride)) - : require('./binding').default; -function toKotlin(options) { - const result = Promise.resolve({}); - const { input, output } = options; - if (!(input === null || input === void 0 ? void 0 : input.root)) { - return result; +exports.runBuild = exports.runDev = void 0; +const fs_1 = __importDefault(require("fs")); +const path_1 = __importDefault(require("path")); +const fast_glob_1 = __importDefault(require("fast-glob")); +const chokidar_1 = require("chokidar"); +const api_1 = require("./api"); +function resolveDefaultOutputDir(mode, inputDir) { + return path_1.default.resolve(inputDir, '../dist/' + mode + '/kotlin'); +} +function parseOptions(mode, opts) { + const { input } = opts; + if (!(input === null || input === void 0 ? void 0 : input.dir)) { + throw new Error(`input.dir is required`); } - if (!(input === null || input === void 0 ? void 0 : input.filename)) { - return result; + if (!fs_1.default.existsSync(input.dir)) { + throw new Error(`${input} is not found`); } - if (!(output === null || output === void 0 ? void 0 : output.outDir)) { - return result; + if (!opts.output) { + opts.output = { + dir: '', + sourceMap: '', + }; } - return bindings.toKotlin(toBuffer(options)); + if (!opts.output.dir) { + opts.output.dir = resolveDefaultOutputDir(mode, input.dir); + } + return opts; +} +const EXTNAME = '.uts'; +function watchSwift(_) { } +function buildSwift(_) { } +function watchKotlin({ input: { dir: inputDir, extname }, output: { dir: outputDir, sourceMap }, }) { + const input = { + root: inputDir, + filename: '', + }; + const output = { + outDir: outputDir, + sourceMap, + }; + (0, chokidar_1.watch)('**/*' + (extname || EXTNAME), { + cwd: inputDir, + ignored: ['**/*.d' + (extname || EXTNAME)], + }) + .on('add', (filename) => buildKotlinFile(path_1.default.resolve(inputDir, filename), input, output)) + .on('change', (filename) => buildKotlinFile(path_1.default.resolve(inputDir, filename), input, output)) + .on('unlink', (filename) => { + try { + fs_1.default.unlinkSync(path_1.default.resolve(outputDir, filename)); + } + catch (e) { } + }); } -exports.toKotlin = toKotlin; -function toSwift(options) { - return bindings.toSwift(toBuffer(options)); +function buildKotlin({ input: { dir: inputDir, extname }, output: { dir: outputDir, sourceMap, inlineSourcesContent }, }) { + const files = fast_glob_1.default.sync('**/*' + (extname || EXTNAME), { + absolute: true, + cwd: inputDir, + ignore: ['**/*.d' + (extname || EXTNAME)], + }); + const input = { + root: inputDir, + filename: '', + }; + const output = { + outDir: outputDir, + sourceMap, + inlineSourcesContent: !!inlineSourcesContent, + }; + return Promise.all(files.map((filename) => buildKotlinFile(filename, input, output))); } -exports.toSwift = toSwift; -function toBuffer(t) { - return Buffer.from(JSON.stringify(t)); +function buildKotlinFile(filename, input, output) { + const label = path_1.default.posix.relative(input.root, filename); + const toKotlinOptions = { + input: { + ...input, + filename, + namespace: '', + }, + output: { + ...output, + }, + }; + console.time(label); + return (0, api_1.toKotlin)(toKotlinOptions).then(() => { + console.timeEnd(label); + }); +} +function runDev(target, opts) { + opts = parseOptions('dev', opts); + switch (target) { + case 'kotlin': + return watchKotlin(opts); + case 'swift': + return watchSwift(opts); + } +} +exports.runDev = runDev; +function runBuild(target, opts) { + opts = parseOptions('build', opts); + switch (target) { + case 'kotlin': + return buildKotlin(opts); + case 'swift': + return buildSwift(opts); + } } +exports.runBuild = runBuild; diff --git a/packages/uts/dist/types.d.ts b/packages/uts/dist/types.d.ts index bc0f9fa5cd1b92784edcf3a8a561f1dec2496443..8d9814cebcea55e05bf3eca8c1649bcefbb74ac2 100644 --- a/packages/uts/dist/types.d.ts +++ b/packages/uts/dist/types.d.ts @@ -29,6 +29,7 @@ export declare type InputKotlinOptions = UtsParseOptions & { export declare type OutputKotlinOptions = { outDir: string; sourceMap: boolean | string; + inlineSourcesContent?: boolean; }; export interface UtsKotlinOptions { input: InputKotlinOptions; diff --git a/packages/uts/src/api.ts b/packages/uts/src/api.ts new file mode 100644 index 0000000000000000000000000000000000000000..ceb549653acfbedc1cadc815c4e149fb27f34fdb --- /dev/null +++ b/packages/uts/src/api.ts @@ -0,0 +1,37 @@ +import { resolve } from 'path' +import { UtsKotlinOptions, UtsResult, UtsSwiftOptions } from './types' + +export * from './types' + +const bindingsOverride = process.env['UTS_BINARY_PATH'] +const bindings = !!bindingsOverride + ? require(resolve(bindingsOverride)) + : require('./binding').default + +export function toKotlin(options: UtsKotlinOptions): Promise { + const result = Promise.resolve({}) + const { input, output } = options + if (!input?.root) { + return result + } + if (!input?.filename) { + return result + } + if (!output?.outDir) { + return result + } + if (output.sourceMap === true) { + output.sourceMap = output.outDir + } else if (output.sourceMap === false) { + output.sourceMap = '' + } + return bindings.toKotlin(toBuffer(options)) +} + +export function toSwift(options: UtsSwiftOptions): Promise { + return bindings.toSwift(toBuffer(options)) +} + +function toBuffer(t: any): Buffer { + return Buffer.from(JSON.stringify(t)) +} diff --git a/packages/uts/src/cli/action.ts b/packages/uts/src/cli/action.ts deleted file mode 100644 index b21c84e520c1d0f3471895616e537d04f3fd5c6d..0000000000000000000000000000000000000000 --- a/packages/uts/src/cli/action.ts +++ /dev/null @@ -1,142 +0,0 @@ -import fs from 'fs' -import path from 'path' -import glob from 'fast-glob' -import { watch } from 'chokidar' - -import { InputKotlinOptions, toKotlin } from '../index' -import { OutputKotlinOptions, UtsKotlinOptions } 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) { - 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 -} - -const EXTNAME = '.uts' - -function watchSwift(_: ToSwiftOptions) {} -function buildSwift(_: ToSwiftOptions) {} -function watchKotlin({ - input: { dir: inputDir, extname }, - output: { dir: outputDir, sourceMap }, -}: ToKotlinOptions) { - const input: InputKotlinOptions = { - root: inputDir, - filename: '', - } - const output: OutputKotlinOptions = { - outDir: outputDir, - sourceMap, - } - watch('**/*' + (extname || EXTNAME), { - cwd: inputDir, - ignored: ['**/*.d' + (extname || EXTNAME)], - }) - .on('add', (filename) => - buildKotlinFile(path.resolve(inputDir, filename), input, output) - ) - .on('change', (filename) => - buildKotlinFile(path.resolve(inputDir, filename), input, output) - ) - .on('unlink', (filename) => { - try { - fs.unlinkSync(path.resolve(outputDir, filename)) - } catch (e) {} - }) -} -function buildKotlin({ - input: { dir: inputDir, extname }, - output: { dir: outputDir, sourceMap }, -}: ToKotlinOptions) { - const files = glob.sync('**/*' + (extname || EXTNAME), { - absolute: true, - cwd: inputDir, - ignore: ['**/*.d' + (extname || EXTNAME)], - }) - 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) - const toKotlinOptions: UtsKotlinOptions = { - input: { - ...input, - filename, - namespace: '', - }, - output: { - ...output, - }, - } - console.time(label) - return toKotlin(toKotlinOptions).then(() => { - 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) - } -} diff --git a/packages/uts/src/cli/index.ts b/packages/uts/src/cli/index.ts index fd430349afd866427c4b4227ecc201abf55a1a4d..d7961e27664da34b0c0709e7da80a80377e0b708 100644 --- a/packages/uts/src/cli/index.ts +++ b/packages/uts/src/cli/index.ts @@ -1,43 +1,51 @@ import { cac } from 'cac' -import { runBuild, runDev, ToOptions } from './action' +import { runBuild, runDev, ToOptions } from '..' const cli = cac('uts') export interface CliOptions { target: 'kotlin' | 'swift' - output: string - sourcemap: boolean + sourceMap: boolean + inlineSourcesContent: boolean watch: boolean extname: string } cli - .command('') + .command(' [output]') .option('-t, --target ', '[string] kotlin | swift', { default: 'kotlin', }) - .option('-o, --output ', `[string] output dir path`) - .option('-s, --sourcemap', `[boolean] output sourcemap`, { + .option('-s, --sourceMap [sourceMap]', `[boolean] output sourceMap`, { default: false, }) + .option( + '-i, --inlineSourcesContent [inlineSourcesContent]', + `[boolean] inline sources content`, + { + default: false, + } + ) .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) => { + .action((input, output, opts: CliOptions) => { const toOptions: ToOptions = { watch: opts.watch, input: { - dir: root, + dir: input, extname: opts.extname, }, output: { - dir: opts.output, - sourceMap: opts.sourcemap, + dir: output, + sourceMap: opts.sourceMap, + inlineSourcesContent: opts.inlineSourcesContent, }, } + console.log(opts, toOptions) return opts.watch ? runDev(opts.target, toOptions) : runBuild(opts.target, toOptions) diff --git a/packages/uts/src/index.ts b/packages/uts/src/index.ts index 0970ee96b9bbc8d476da0e8055c9ab8edaacb9df..555755594b8666f01abf1d6e794f7c49ed8d923b 100644 --- a/packages/uts/src/index.ts +++ b/packages/uts/src/index.ts @@ -1,33 +1,144 @@ -import { resolve } from 'path' -import { UtsKotlinOptions, UtsResult, UtsSwiftOptions } from './types' +import fs from 'fs' +import path from 'path' +import glob from 'fast-glob' +import { watch } from 'chokidar' -export * from './types' +import { InputKotlinOptions, toKotlin } from './api' +import { OutputKotlinOptions, UtsKotlinOptions } from './types' -// Allow overrides to the location of the .node binding file -const bindingsOverride = process.env['UTS_BINARY_PATH'] -const bindings = !!bindingsOverride - ? require(resolve(bindingsOverride)) - : require('./binding').default +export interface ToOptions { + watch?: boolean + input: { + dir: string + extname?: string + } + output: { + dir: string + sourceMap: boolean | string + inlineSourcesContent?: boolean + } +} + +interface ToKotlinOptions extends ToOptions {} +interface ToSwiftOptions extends ToOptions {} + +function resolveDefaultOutputDir(mode: 'dev' | 'build', inputDir: string) { + return path.resolve(inputDir, '../dist/' + mode + '/kotlin') +} +function parseOptions(mode: 'dev' | 'build', opts: Partial) { + 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: '', + } + } + if (!opts.output.dir) { + opts.output.dir = resolveDefaultOutputDir(mode, input.dir) + } + return opts as ToKotlinOptions +} + +const EXTNAME = '.uts' -export function toKotlin(options: UtsKotlinOptions): Promise { - const result = Promise.resolve({}) - const { input, output } = options - if (!input?.root) { - return result +function watchSwift(_: ToSwiftOptions) {} +function buildSwift(_: ToSwiftOptions) {} +function watchKotlin({ + input: { dir: inputDir, extname }, + output: { dir: outputDir, sourceMap }, +}: ToKotlinOptions) { + const input: InputKotlinOptions = { + root: inputDir, + filename: '', + } + const output: OutputKotlinOptions = { + outDir: outputDir, + sourceMap, + } + watch('**/*' + (extname || EXTNAME), { + cwd: inputDir, + ignored: ['**/*.d' + (extname || EXTNAME)], + }) + .on('add', (filename) => + buildKotlinFile(path.resolve(inputDir, filename), input, output) + ) + .on('change', (filename) => + buildKotlinFile(path.resolve(inputDir, filename), input, output) + ) + .on('unlink', (filename) => { + try { + fs.unlinkSync(path.resolve(outputDir, filename)) + } catch (e) {} + }) +} +function buildKotlin({ + input: { dir: inputDir, extname }, + output: { dir: outputDir, sourceMap, inlineSourcesContent }, +}: ToKotlinOptions) { + const files = glob.sync('**/*' + (extname || EXTNAME), { + absolute: true, + cwd: inputDir, + ignore: ['**/*.d' + (extname || EXTNAME)], + }) + const input: InputKotlinOptions = { + root: inputDir, + filename: '', } - if (!input?.filename) { - return result + const output: OutputKotlinOptions = { + outDir: outputDir, + sourceMap, + inlineSourcesContent: !!inlineSourcesContent, } - if (!output?.outDir) { - return result + 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) + const toKotlinOptions: UtsKotlinOptions = { + input: { + ...input, + filename, + namespace: '', + }, + output: { + ...output, + }, } - return bindings.toKotlin(toBuffer(options)) + console.time(label) + return toKotlin(toKotlinOptions).then(() => { + console.timeEnd(label) + }) } -export function toSwift(options: UtsSwiftOptions): Promise { - return bindings.toSwift(toBuffer(options)) +export function runDev(target: 'kotlin' | 'swift', opts: ToOptions) { + opts = parseOptions('dev', opts) + switch (target) { + case 'kotlin': + return watchKotlin(opts) + case 'swift': + return watchSwift(opts) + } } -function toBuffer(t: any): Buffer { - return Buffer.from(JSON.stringify(t)) +export function runBuild(target: 'kotlin' | 'swift', opts: ToOptions) { + opts = parseOptions('build', opts) + switch (target) { + case 'kotlin': + return buildKotlin(opts) + case 'swift': + return buildSwift(opts) + } } diff --git a/packages/uts/src/types.ts b/packages/uts/src/types.ts index 54c74bf92139d47e9ef178aa3863018df6c894c3..117572d60bb84a6a025ea059e909bebcac43713a 100644 --- a/packages/uts/src/types.ts +++ b/packages/uts/src/types.ts @@ -43,6 +43,7 @@ export type InputKotlinOptions = UtsParseOptions & { export type OutputKotlinOptions = { outDir: string sourceMap: boolean | string + inlineSourcesContent?: boolean } export interface UtsKotlinOptions { input: InputKotlinOptions