1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | const fs = require('fs');
|
14 | const path = require('path');
|
15 | const inquirer = require('inquirer');
|
16 |
|
17 | const ZIP_NAME_REGEX = /\.zip$/;
|
18 |
|
19 |
|
20 |
|
21 | const PACKAGER_ZIP_NAME_REGEX = /^package-.*-.*\.zip$/;
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | const 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 |
|
58 | const 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 |
|
72 | const 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 |
|
92 | const 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 |
|
105 | const 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 |
|
116 | module.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 | };
|