UNPKG

4.01 kBJavaScriptView Raw
1/***************************************************************************************
2 * (c) 2017 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 ****************************************************************************************/
12
13const fs = require('fs');
14const path = require('path');
15const inquirer = require('inquirer');
16
17const ZIP_NAME_REGEX = /\.zip$/;
18
19// The file name pattern that reactor-packager uses:
20// https://www.npmjs.com/package/@adobe/reactor-packager
21const PACKAGER_ZIP_NAME_REGEX = /^package-.*-.*\.zip$/;
22
23// Find zip files in the working directory. Then, sort any of the files that look like they
24// came from the packager to the top of the list. Then, sort by the date the files were
25// last changed (most recent first). This will hopefully bring the most relevant files to the
26// top of the list.
27const getZipsInDir = () => {
28 return fs.readdirSync(process.cwd())
29 .filter(file => ZIP_NAME_REGEX.test(file))
30 .sort((fileA, fileB) => {
31 const fileAIsFromPackager = PACKAGER_ZIP_NAME_REGEX.test(fileA);
32 const fileBIsFromPackager = PACKAGER_ZIP_NAME_REGEX.test(fileB);
33
34 if (fileAIsFromPackager && !fileBIsFromPackager) {
35 return -1;
36 }
37
38 if (!fileAIsFromPackager && fileBIsFromPackager) {
39 return 1;
40 }
41
42 const fileATime = fs.statSync(path.resolve(fileA)).ctimeMs;
43 const fileBTime = fs.statSync(path.resolve(fileB)).ctimeMs;
44
45 if (fileATime > fileBTime) {
46 return -1;
47 }
48
49 if (fileATime < fileBTime) {
50 return 1;
51 }
52
53 return 0;
54 });
55};
56
57
58const proposeSingleZip = async (candidateZip) => {
59 const { correctZip } = await inquirer.prompt([
60 {
61 type: 'confirm',
62 name: 'correctZip',
63 message: `The zip file ${candidateZip} was found in the current directory. Is this the zip file you would like to upload?`
64 }
65 ]);
66
67 if (correctZip) {
68 return candidateZip;
69 }
70};
71
72const proposeMultipleZips = async (zipsInDir) => {
73 const NONE_OPTION = 'None of the files listed';
74 const { zipPath } = await inquirer.prompt([
75 {
76 type: 'list',
77 name: 'zipPath',
78 message: 'Which of the following zip files would you like to upload?',
79 choices: zipsInDir.concat([
80 new inquirer.Separator(),
81 NONE_OPTION,
82 new inquirer.Separator()
83 ])
84 }
85 ]);
86
87 if (zipPath !== NONE_OPTION) {
88 return zipPath;
89 }
90};
91
92const getUserEnteredZip = async () => {
93 const { zipPath } = await inquirer.prompt([
94 {
95 type: 'input',
96 name: 'zipPath',
97 message: 'Please enter the path (relative or absolute) to the zip file you would like to upload.',
98 validate: Boolean
99 }
100 ]);
101
102 return validateZipPath(zipPath) ? zipPath : await getUserEnteredZip();
103};
104
105const validateZipPath = (zipPath) => {
106 const absPath = path.resolve(zipPath);
107
108 if (fs.existsSync(absPath)) {
109 return true;
110 } else {
111 console.error(`No file found at ${absPath}`);
112 return false;
113 }
114};
115
116module.exports = async (argv) => {
117 let zipPath;
118
119 const zipArg = argv._[0];
120
121 if (zipArg && validateZipPath(zipArg)) {
122 zipPath = zipArg;
123 } else {
124 const zipsInDir = getZipsInDir();
125
126 if (zipsInDir.length === 1) {
127 zipPath = await proposeSingleZip(zipsInDir[0]);
128 } else if (zipsInDir.length > 1) {
129 zipPath = await proposeMultipleZips(zipsInDir);
130 }
131 }
132
133 if (!zipPath) {
134 zipPath = await getUserEnteredZip();
135 }
136
137 return path.resolve(zipPath);
138};