summaryrefslogtreecommitdiffstats
path: root/src/arrow/js/gulp/util.js
blob: d8cde29e8fa78fd703333442fa748de58d7ce531 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

const fs = require('fs');
const path = require(`path`);
const pump = require(`stream`).pipeline;
const child_process = require(`child_process`);
const { targets, modules } = require('./argv');
const {
    ReplaySubject,
    empty: ObservableEmpty,
    throwError: ObservableThrow,
    fromEvent: ObservableFromEvent
} = require('rxjs');
const {
    share,
    flatMap,
    takeUntil,
    defaultIfEmpty,
    mergeWith,
} = require('rxjs/operators');
const asyncDone = require('util').promisify(require('async-done'));

const mainExport = `Arrow`;
const npmPkgName = `apache-arrow`;
const npmOrgName = `@${npmPkgName}`;

const releasesRootDir = `targets`;
const knownTargets = [`es5`, `es2015`, `esnext`];
const knownModules = [`cjs`, `esm`, `cls`, `umd`];
const tasksToSkipPerTargetOrFormat = {
    src: { clean: true, build: true },
    cls: { test: true, package: true }
};
const packageJSONFields = [
  `version`, `license`, `description`,
  `author`, `homepage`, `repository`,
  `bugs`, `keywords`,  `dependencies`,
  `bin`
];

const metadataFiles = [`LICENSE.txt`, `NOTICE.txt`, `README.md`].map((filename) => {
    let prefixes = [`./`, `../`];
    let p = prefixes.find((prefix) => {
        try {
            fs.statSync(path.resolve(path.join(prefix, filename)));
        } catch (e) { return false; }
        return true;
    });
    if (!p) {
        throw new Error(`Couldn't find ${filename} in ./ or ../`);
    }
    return path.join(p, filename);
});

// see: https://github.com/google/closure-compiler/blob/c1372b799d94582eaf4b507a4a22558ff26c403c/src/com/google/javascript/jscomp/CompilerOptions.java#L2988
const gCCLanguageNames = {
    es5: `ECMASCRIPT5`,
 es2015: `ECMASCRIPT_2015`,
 es2016: `ECMASCRIPT_2016`,
 es2017: `ECMASCRIPT_2017`,
 es2018: `ECMASCRIPT_2018`,
 es2019: `ECMASCRIPT_2019`,
 esnext: `ECMASCRIPT_NEXT`
};

function taskName(target, format) {
    return !format ? target : `${target}:${format}`;
}

function packageName(target, format) {
    return !format ? target : `${target}-${format}`;
}

function tsconfigName(target, format) {
    return !format ? target : `${target}.${format}`;
}

function targetDir(target, format) {
    return path.join(releasesRootDir, ...(!format ? [target] : [target, format]));
}

function shouldRunInChildProcess(target, format) {
    // If we're building more than one module/target, then yes run this task in a child process
    if (targets.length > 1 || modules.length > 1) { return true; }
    // If the target we're building *isn't* the target the gulp command was configured to run, then yes run that in a child process
    if (targets[0] !== target || modules[0] !== format) { return true; }
    // Otherwise no need -- either gulp was run for just one target, or we've been spawned as the child of a multi-target parent gulp
    return false;
}

const gulp = path.join(path.parse(require.resolve(`gulp`)).dir, `bin/gulp.js`);
function spawnGulpCommandInChildProcess(command, target, format) {
    const args = [gulp, command, '-t', target, '-m', format, `--silent`];
    const opts = {
        stdio: [`ignore`, `inherit`, `inherit`],
        env: { ...process.env, NODE_NO_WARNINGS: `1` }
    };
    return asyncDone(() => child_process.spawn(`node`, args, opts))
        .catch((e) => { throw `Error in "${command}:${taskName(target, format)}" task`; });
}

const logAndDie = (e) => { if (e) { process.exit(1) } };
function observableFromStreams(...streams) {
    if (streams.length <= 0) { return ObservableEmpty(); }
    const pumped = streams.length <= 1 ? streams[0] : pump(...streams, logAndDie);
    const fromEvent = ObservableFromEvent.bind(null, pumped);
    const streamObs = fromEvent(`data`).pipe(
        mergeWith(fromEvent(`error`).pipe(flatMap((e) => ObservableThrow(e)))),
        takeUntil(fromEvent(`end`).pipe(mergeWith(fromEvent(`close`)))),
        defaultIfEmpty(`empty stream`),
        share({ connector: () => new ReplaySubject(), resetOnError: false, resetOnComplete: false, resetOnRefCountZero: false })
    );
    streamObs.stream = pumped;
    streamObs.observable = streamObs;
    return streamObs;
}

function* combinations(_targets, _modules) {
    const targets = known(knownTargets, _targets || [`all`]);
    const modules = known(knownModules, _modules || [`all`]);

    if (_targets.includes(`src`)) {
        yield [`src`, ``];
        return;
    }

    if (_targets.includes(`all`) && _modules.includes(`all`)) {
        yield [`ts`, ``];
        yield [`src`, ``];
        yield [npmPkgName, ``];
    }

    for (const format of modules) {
        for (const target of targets) {
            yield [target, format];
        }
    }

    function known(known, values) {
        return values.includes(`all`) ? known
            :  values.includes(`src`) ? [`src`]
            : Object.keys(
                values.reduce((map, arg) => ((
                    (known.includes(arg)) &&
                    (map[arg.toLowerCase()] = true)
                    || true) && map
                ), {})
            ).sort((a, b) => known.indexOf(a) - known.indexOf(b));
    }
}

const publicModulePaths = (dir) => [
    `${dir}/${mainExport}.dom.js`,
    `${dir}/util/int.js`,
    `${dir}/compute/predicate.js`,
];

const esmRequire = require(`esm`)(module, {
    mode: `auto`,
    cjs: {
        /* A boolean for storing ES modules in require.cache. */
        cache: true,
        /* A boolean for respecting require.extensions in ESM. */
        extensions: true,
        /* A boolean for __esModule interoperability. */
        interop: true,
        /* A boolean for importing named exports of CJS modules. */
        namedExports: true,
        /* A boolean for following CJS path rules in ESM. */
        paths: true,
        /* A boolean for __dirname, __filename, and require in ESM. */
        vars: true,
    }
});

module.exports = {
    mainExport, npmPkgName, npmOrgName, metadataFiles, packageJSONFields,

    knownTargets, knownModules, tasksToSkipPerTargetOrFormat, gCCLanguageNames,

    taskName, packageName, tsconfigName, targetDir, combinations, observableFromStreams,
    publicModulePaths, esmRequire, shouldRunInChildProcess, spawnGulpCommandInChildProcess,

    targetAndModuleCombinations: [...combinations(targets, modules)]
};