提交 08b45f6b 编写于 作者: fxy060608's avatar fxy060608

wip(uts): api

上级 8c4af279
import { UtsKotlinOptions, UtsResult, UtsSwiftOptions } from './types';
export * from './types';
export declare function toKotlin(options: UtsKotlinOptions): Promise<UtsResult>;
export declare function toSwift(options: UtsSwiftOptions): Promise<UtsResult>;
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<void[]>;
"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;
......@@ -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;
......
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<UtsResult> {
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<UtsResult> {
return bindings.toSwift(toBuffer(options))
}
function toBuffer(t: any): Buffer {
return Buffer.from(JSON.stringify(t))
}
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<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
}
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)
}
}
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('<root>')
.command('<input> [output]')
.option('-t, --target <target>', '[string] kotlin | swift', {
default: 'kotlin',
})
.option('-o, --output <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)
......
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<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: '',
}
}
if (!opts.output.dir) {
opts.output.dir = resolveDefaultOutputDir(mode, input.dir)
}
return opts as ToKotlinOptions
}
const EXTNAME = '.uts'
export function toKotlin(options: UtsKotlinOptions): Promise<UtsResult> {
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<UtsResult> {
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)
}
}
......@@ -43,6 +43,7 @@ export type InputKotlinOptions = UtsParseOptions & {
export type OutputKotlinOptions = {
outDir: string
sourceMap: boolean | string
inlineSourcesContent?: boolean
}
export interface UtsKotlinOptions {
input: InputKotlinOptions
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册