UNPKG

3.33 kBJavaScriptView Raw
1/*
2 * Copyright 2019 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12const path = require('path');
13const fse = require('fs-extra');
14const shell = require('shelljs');
15const chalk = require('chalk');
16const { GitUrl } = require('@adobe/helix-shared');
17
18function execAsync(cmd) {
19 return new Promise((resolve, reject) => {
20 shell.exec(cmd, (code, stdout, stderr) => {
21 if (code === 0) {
22 resolve(0);
23 } else {
24 reject(stderr);
25 }
26 });
27 });
28}
29
30/**
31 * Utilities for helix pages
32 */
33class HelixPages {
34 constructor(logger) {
35 this._logger = logger;
36 this._cwd = process.cwd();
37 this._repo = 'https://github.com/adobe/helix-pages.git';
38 this._ref = 'master';
39 }
40
41 withDirectory(value) {
42 this._cwd = value;
43 return this;
44 }
45
46 withRepo(value) {
47 this._repo = value;
48 }
49
50 get log() {
51 return this._logger;
52 }
53
54 get directory() {
55 return this._cwd;
56 }
57
58 get homeDirectory() {
59 return this._homeDirectory;
60 }
61
62 get srcDirectory() {
63 return this._srcDirectory;
64 }
65
66 get checkoutDirectory() {
67 return this._checkoutDir;
68 }
69
70 get staticURL() {
71 return this._staticURL;
72 }
73
74 async isPagesProject() {
75 return !await fse.pathExists(path.join(this.directory, 'src'));
76 }
77
78 async init() {
79 // check if helix-pages checkout exists
80 // todo: add support to check for updates and version control
81 this._homeDirectory = path.resolve(this.directory, '.hlx', 'pages');
82 this._checkoutDir = path.resolve(this.homeDirectory, this._ref);
83 this._srcDirectory = path.resolve(this.checkoutDirectory, 'src');
84 this._staticURL = new GitUrl('https://github.com/adobe/helix-pages.git/htdocs');
85 }
86
87 async prepare() {
88 if (!await fse.pathExists(this.checkoutDirectory)) {
89 this.log.info(chalk`Checking out sources from {cyan ${this._repo}#${this._ref}}`);
90 try {
91 await execAsync(`git clone --branch ${this._ref} --quiet --depth 1 ${this._repo} ${this.checkoutDirectory}`);
92 } catch (e) {
93 throw Error(`Unable to checkout helix-pages repository: ${e}`);
94 }
95
96 const pagesPackageJson = path.resolve(this.checkoutDirectory, 'package.json');
97 if (await fse.pathExists(pagesPackageJson)) {
98 const pkgJson = await fse.readJson(pagesPackageJson);
99 this.log.info(chalk`Running {gray npm install} for {yellow ${pkgJson.name}@${pkgJson.version}}...`);
100 const cwd = process.cwd();
101 try {
102 shell.cd(this.checkoutDirectory);
103 await execAsync('npm install --only=prod --ignore-scripts --no-bin-links --no-audit');
104 } catch (e) {
105 throw Error(`Unable to install helix-pages dependencies: ${e}`);
106 } finally {
107 shell.cd(cwd);
108 }
109 }
110 }
111 }
112}
113
114module.exports = HelixPages;