1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | const path = require('path');
|
13 | const fse = require('fs-extra');
|
14 | const shell = require('shelljs');
|
15 | const chalk = require('chalk');
|
16 | const { GitUrl } = require('@adobe/helix-shared');
|
17 |
|
18 | function 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 |
|
32 |
|
33 | class 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 |
|
80 |
|
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 |
|
114 | module.exports = HelixPages;
|