index.js 3.9 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
exports.runBuild = exports.runDev = void 0;
const path_1 = __importDefault(require("path"));
fxy060608's avatar
fxy060608 已提交
8
const fs_extra_1 = __importDefault(require("fs-extra"));
fxy060608's avatar
fxy060608 已提交
9 10 11
const fast_glob_1 = __importDefault(require("fast-glob"));
const chokidar_1 = require("chokidar");
const api_1 = require("./api");
fxy060608's avatar
fxy060608 已提交
12
const utils_1 = require("./utils");
fxy060608's avatar
fxy060608 已提交
13 14 15 16 17 18 19
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 已提交
20
    }
fxy060608's avatar
fxy060608 已提交
21
    if (!fs_extra_1.default.existsSync(input.dir)) {
fxy060608's avatar
fxy060608 已提交
22
        throw new Error(`${input} is not found`);
fxy060608's avatar
fxy060608 已提交
23
    }
fxy060608's avatar
fxy060608 已提交
24 25 26 27 28
    if (!opts.output) {
        opts.output = {
            dir: '',
            sourceMap: '',
        };
fxy060608's avatar
fxy060608 已提交
29
    }
fxy060608's avatar
fxy060608 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
    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,
    };
fxy060608's avatar
fxy060608 已提交
47 48
    extname = extname || EXTNAME;
    (0, chokidar_1.watch)('**/*' + extname, {
fxy060608's avatar
fxy060608 已提交
49
        cwd: inputDir,
fxy060608's avatar
fxy060608 已提交
50
        ignored: ['**/*.d' + extname],
fxy060608's avatar
fxy060608 已提交
51 52 53 54 55
    })
        .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 {
fxy060608's avatar
fxy060608 已提交
56
            fs_extra_1.default.unlinkSync(path_1.default.resolve(outputDir, filename));
fxy060608's avatar
fxy060608 已提交
57 58
        }
        catch (e) { }
fxy060608's avatar
fxy060608 已提交
59 60 61
    })
        .on('ready', () => {
        copyAssets(inputDir, outputDir, extname);
fxy060608's avatar
fxy060608 已提交
62
    });
fxy060608's avatar
fxy060608 已提交
63
}
fxy060608's avatar
fxy060608 已提交
64
function buildKotlin({ input: { dir: inputDir, extname }, output: { dir: outputDir, sourceMap, inlineSourcesContent }, }) {
fxy060608's avatar
fxy060608 已提交
65 66
    extname = extname || EXTNAME;
    const files = fast_glob_1.default.sync('**/*' + extname, {
fxy060608's avatar
fxy060608 已提交
67 68
        absolute: true,
        cwd: inputDir,
fxy060608's avatar
fxy060608 已提交
69
        ignore: ['**/*.d' + extname],
fxy060608's avatar
fxy060608 已提交
70 71 72 73 74 75 76 77 78 79
    });
    const input = {
        root: inputDir,
        filename: '',
    };
    const output = {
        outDir: outputDir,
        sourceMap,
        inlineSourcesContent: !!inlineSourcesContent,
    };
fxy060608's avatar
fxy060608 已提交
80 81 82 83 84 85 86 87 88 89
    return Promise.all(files.map((filename) => buildKotlinFile(filename, input, output))).then(() => {
        return copyAssets(inputDir, outputDir, extname);
    });
}
function copyAssets(inputDir, outputDir, extname) {
    return fs_extra_1.default.copy(inputDir, outputDir, {
        filter(src) {
            return path_1.default.extname(src) !== extname;
        },
    });
fxy060608's avatar
fxy060608 已提交
90
}
fxy060608's avatar
fxy060608 已提交
91
function buildKotlinFile(filename, input, output) {
fxy060608's avatar
fxy060608 已提交
92
    const label = (0, utils_1.normalizePath)(path_1.default.relative(input.root, filename));
fxy060608's avatar
fxy060608 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    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 已提交
126
}
fxy060608's avatar
fxy060608 已提交
127
exports.runBuild = runBuild;