@adpt/cloud
Version:
AdaptJS cloud component library
86 lines • 3.45 kB
JavaScript
;
/*
* Copyright 2019 Unbounded Systems, LLC
*
* Licensed 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const core_1 = tslib_1.__importStar(require("@adpt/core"));
const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
const lodash_1 = require("lodash");
const path_1 = tslib_1.__importDefault(require("path"));
const docker_1 = require("../docker");
const env_1 = require("../env");
function argLines(env) {
const pairs = env_1.mergeEnvPairs(env);
if (!pairs)
return "";
const lines = pairs.map((v) => `ARG ${v.name}`);
return lines.join("\n");
}
/**
* Locally builds a docker image for a {@link https://www.nodejs.org | Node.js} program.
*
* @remarks
* Implements {@link docker.DockerImageInstance}.
*
* See {@link nodejs.LocalNodeImageProps}.
*
* @public
*/
function LocalNodeImage(props) {
const opts = Object.assign({}, defaultContainerBuildOptions, (props.options || {}));
const [imgProps, setImgProps] = core_1.useState(undefined);
const img = core_1.handle();
setImgProps(async () => {
const srcDir = path_1.default.resolve(props.srcDir);
if (!(await fs_extra_1.default.pathExists(srcDir)))
throw new Error(`Source directory ${srcDir} not found`);
const pkgInfo = await fs_extra_1.default.readJson(path_1.default.join(srcDir, "package.json"));
const main = pkgInfo.main ? pkgInfo.main : "index.js";
const runNpmScripts = opts.runNpmScripts;
const scripts = lodash_1.isArray(runNpmScripts) ? runNpmScripts :
lodash_1.isString(runNpmScripts) ? [runNpmScripts] :
[];
const runCommands = scripts.map((s) => `RUN ${opts.packageManager} run ${s}`).join("\n");
return {
dockerfile: `
FROM node:10-stretch-slim
ENV TINI_VERSION v0.18.0
ADD https://github.com/krallin/tini/releases/download/\${TINI_VERSION}/tini /tini
ENTRYPOINT ["/tini", "--"]
${argLines(opts.buildArgs)}
WORKDIR /app
ADD . /app
RUN ${opts.packageManager} install && chmod +x /tini
${runCommands}
CMD ["node", "${main}"]
`,
contextDir: srcDir,
options: opts,
};
});
core_1.useMethodFrom(img, "image");
core_1.useMethodFrom(img, "latestImage");
core_1.useMethodFrom(img, "pushTo");
return imgProps ? core_1.default.createElement(docker_1.LocalDockerImage, Object.assign({ handle: img }, imgProps)) : null;
}
exports.LocalNodeImage = LocalNodeImage;
const defaultContainerBuildOptions = {
imageName: "node-service",
packageManager: "npm",
uniqueTag: true,
buildArgs: {}
};
//# sourceMappingURL=LocalNodeImage.js.map