index.js 3.5 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
"use strict";
fxy060608's avatar
fxy060608 已提交
2 3
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
fxy060608's avatar
fxy060608 已提交
4 5
};
Object.defineProperty(exports, "__esModule", { value: true });
fxy060608's avatar
fxy060608 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18
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`);
fxy060608's avatar
fxy060608 已提交
19
    }
fxy060608's avatar
fxy060608 已提交
20 21
    if (!fs_1.default.existsSync(input.dir)) {
        throw new Error(`${input} is not found`);
fxy060608's avatar
fxy060608 已提交
22
    }
fxy060608's avatar
fxy060608 已提交
23 24 25 26 27
    if (!opts.output) {
        opts.output = {
            dir: '',
            sourceMap: '',
        };
fxy060608's avatar
fxy060608 已提交
28
    }
fxy060608's avatar
fxy060608 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
    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) { }
    });
fxy060608's avatar
fxy060608 已提交
58
}
fxy060608's avatar
fxy060608 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
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)));
fxy060608's avatar
fxy060608 已提交
75
}
fxy060608's avatar
fxy060608 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
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);
    }
fxy060608's avatar
fxy060608 已提交
111
}
fxy060608's avatar
fxy060608 已提交
112
exports.runBuild = runBuild;