UNPKG

3.44 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3const program = require('commander'),
4 fs = require('fs'),
5 paths = require('path'),
6 shell = require('shelljs'),
7 glob = require('glob'),
8 archiver = require('archiver'),
9 templates = require('./lib/templates'),
10 logger = require('./lib/logger'),
11 settings = require('./lib/settings'),
12 version = require('./package.json').version;
13
14const ALLOWED_DIRECTORIES = ['marketplace_builder', 'modules'];
15const availableDirectories = () => ALLOWED_DIRECTORIES.filter(fs.existsSync);
16const isEmpty = dir => shell.ls(dir).length == 0;
17
18const addModulesToArchive = archive => {
19 if (!fs.existsSync('modules')) return Promise.resolve(true);
20
21 return Promise.all(
22 glob.sync('*/', { cwd: 'modules' }).map(
23 module => ( addModuleToArchive(module, archive))
24 )
25 );
26};
27
28const addModuleToArchive = (module, archive) => {
29 return new Promise((resolve, reject) => {
30 glob('?(public|private)/**', { cwd: `modules/${module}` }, (err, files) => {
31 if (err) throw reject(err);
32 const moduleTemplateData = templateData(`modules/${module}/template-values.json`);
33
34 return Promise.all(
35 files.map(f => {
36 const path = `modules/${module}/${f}`;
37 return new Promise((resolve, reject) => {
38 fs.lstat(path, (err, stat) => {
39 if (!stat.isDirectory()) {
40 archive.append(templates.fillInTemplateValues(path, moduleTemplateData), {
41 name: path
42 });
43 }
44 resolve();
45 });
46 });
47 })
48 ).then(r => {
49 resolve();
50 });
51 });
52 });
53};
54
55const makeArchive = (path, directory, withoutAssets) => {
56 if (availableDirectories().length === 0) {
57 logger.Error(`At least one of ${ALLOWED_DIRECTORIES} directories is needed to deploy`, { hideTimestamp: true });
58 }
59
60 availableDirectories().map(dir => {
61 if (isEmpty(dir) && !withoutAssets) {
62 logger.Error(`${dir} can't be empty if the deploy is not partial - it would remove all the files from the instance`, {
63 hideTimestamp: true
64 });
65 }
66 });
67
68 shell.mkdir('-p', 'tmp');
69 shell.rm('-rf', path);
70
71 const output = fs.createWriteStream(path);
72 const archive = archiver('zip', { zlib: { level: 6 } });
73
74 // listen for all archive data to be written
75 // 'close' event is fired only when a file descriptor is involved
76 output.on('close', () => {
77 const sizeInMB = archive.pointer() / 1024 / 1024;
78 logger.Info(`Archive size: ${sizeInMB.toFixed(2)} MB`);
79 });
80
81 archive.on('warning', err => {
82 if (err.code === 'ENOENT') {
83 logger.Error(err);
84 } else throw err;
85 });
86
87 archive.on('error', err => {
88 throw err;
89 });
90
91 // pipe archive data to the file
92 archive.pipe(output);
93
94 let options = { cwd: directory };
95 if (withoutAssets) options.ignore = ['assets/**'];
96
97 archive.glob('**/*', options, { prefix: directory });
98
99 addModulesToArchive(archive).then(r => {
100 archive.finalize();
101 });
102};
103
104const templateData = (path) => {
105 return settings.loadSettingsFile(path);
106};
107
108program
109 .version(version)
110 .option('--dir <dir>', 'files to be added to build', 'marketplace_builder')
111 .option('--without-assets', 'if present assets directory will be excluded')
112 .option('--target <target>', 'path to archive', process.env.TARGET || './tmp/marketplace-release.zip')
113 .parse(process.argv);
114
115makeArchive(program.target, program.dir, program.withoutAssets);