Skip to content

Commit 9441621

Browse files
Merge pull request #64 from MaartenDesnouck/next-version
next version
2 parents d66fc17 + 4376688 commit 9441621

20 files changed

+72
-107
lines changed

lib/auth.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,11 @@ module.exports = (options) => {
1919
return getUserInfo(values[0]);
2020
});
2121

22-
const finished = gotUserInfo.then((info) => {
22+
gotUserInfo.then((info) => {
2323
process.stdout.write(`You are successfully authenticated as \'${info.email}\'`);
2424
displayCheckbox('green');
2525
return;
26-
});
27-
28-
// Catch all the errors
29-
Promise.all([gotAuth, checkedVersion, gotUserInfo, finished, ]).catch((err) => {
26+
}).catch((err) => {
3027
handleError(err, true);
3128
return;
3229
});

lib/clone.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,10 @@ module.exports = (identifier) => {
3737
return unpackRemote(values[1].name);
3838
});
3939

40-
const finished = unpacked.then(() => {
40+
unpacked.then(() => {
4141
displayCheckbox('green');
4242
return;
43-
});
44-
45-
// Catch all the errors
46-
Promise.all([gotAuth, gotMetadata, downloaded, unpacked, checkedVersion, finished, ]).catch((err) => {
43+
}).catch((err) => {
4744
handleError(err, true);
4845
return;
4946
});

lib/constants.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const path = require('path');
2+
const os = require('os');
23

34
/**
45
* Define constants
@@ -15,7 +16,7 @@ function Constants() {
1516
],
1617
},
1718
'APP_DIR': {
18-
value: path.join((process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE), '.google-apps-script'),
19+
value: path.join(os.homedir(), '.google-apps-script'),
1920
},
2021
'TOKEN_FILE': {
2122
value: 'token.json',

lib/functions/checkNewVersion.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function checkNewVersion() {
4444
console.log(`${` ┏`.yellow}${`-`.repeat(scale + 45).yellow}${`┓`.yellow}`);
4545
console.log(`${` |`.yellow}${` `.repeat(scale + 45)}${`|`.yellow}`);
4646
console.log(`${` |`.yellow}${` Update available ${pjson.version} -> `}${`${latestVersion}`.green}${` |`.yellow}`);
47-
console.log(`${` |`.yellow}${` Run ` + `npm i -g google-apps-script`.blue}${` to update`}${` `.repeat(scale + 2)}${`|`.yellow}`);
47+
console.log(`${` |`.yellow}${` Run `}${`npm i -g google-apps-script`.blue}${` to update`}${` `.repeat(scale + 2)}${`|`.yellow}`);
4848
console.log(`${` |`.yellow}${` `.repeat(scale + 45)}${`|`.yellow}`);
4949
console.log(`${` ┗`.yellow}${`-`.repeat(scale + 45).yellow}${`┛`.yellow}`);
5050
console.log();

lib/functions/downloadRemote.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,25 @@ function downloadRemote(auth, fileId, dir, method) {
3838
};
3939
createFile(file);
4040

41-
const dest = fs.createWriteStream(path.join(gasDir, constants.META_REMOTE));
41+
const remote = path.join(gasDir, constants.META_REMOTE);
4242
const drive = google.drive('v3');
4343

4444
drive.files.export({
4545
auth,
4646
fileId,
4747
mimeType: constants.MIME_GAS_JSON,
48-
}, (err, result) => {
48+
}, (err, content) => {
4949
if (err) {
50-
fs.unlinkSync(path.join(gasDir, constants.META_REMOTE));
50+
fs.unlinkSync(remote);
5151
reject(err);
5252
return;
53+
} else {
54+
createFile({
55+
name: remote,
56+
source: JSON.stringify(content),
57+
});
58+
resolve();
5359
}
54-
}).pipe(dest);
55-
56-
dest.on('finish', () => {
57-
resolve();
58-
return;
5960
});
6061
});
6162
}

lib/functions/packLocal.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const getAllFiles = require('./getAllFiles.js');
55
const constants = require('../constants.js');
66

77
const local = path.join(constants.META_DIR, constants.META_LOCAL);
8-
const remote = path.join(constants.META_DIR, constants.META_REMOTE);
98

109
/**
1110
* Getting the json form of a file.
@@ -20,7 +19,7 @@ function getFileJSON(file, nameWithoutExtension, extension) {
2019
fs.stat(file, (err, stats) => {
2120
if (stats.isFile()) {
2221
// Read local javascript file
23-
fs.readFile(file, 'utf8', (err, content) => {
22+
fs.readFile(file, 'utf8', (err, source) => {
2423
if (err) {
2524
reject(err);
2625
return;
@@ -30,7 +29,7 @@ function getFileJSON(file, nameWithoutExtension, extension) {
3029
const fileJSON = {
3130
name: nameWithoutExtension,
3231
type,
33-
source: content,
32+
source,
3433
};
3534
resolve(fileJSON);
3635
return;
@@ -53,20 +52,29 @@ function packLocal() {
5352
return new Promise((resolve, reject) => {
5453
const files = getAllFiles('.');
5554
const promises = [];
55+
const filenames = [];
56+
5657
for (const file of files) {
5758
const extension = path.parse(file).ext;
5859
const nameWithoutExtension = path.parse(file).name;
59-
const folder = path.parse(file).dir
60+
const folder = path.parse(file).dir;
6061

6162
// If extension is correct and file does not start with a dot
6263
if ((extension === '.js' || extension === '.html') && (file[0] !== '.')) {
6364
const filename = path.join(folder, nameWithoutExtension).replace(`\\`, `/`);
64-
promises.push(getFileJSON(file, filename, extension));
65+
if (filenames.includes(filename)) {
66+
reject(`Can't construct a Google Apps Script project with files with the same name: '${filename}.*'`);
67+
return;
68+
} else {
69+
filenames.push(filename);
70+
promises.push(getFileJSON(file, filename, extension));
71+
}
72+
6573
}
6674
}
6775

6876
// Reject if there are no correct files
69-
if (promises.length === 0) {
77+
if (filenames.length === 0) {
7078
reject(`Can't construct a Google Apps Script project without .js or .html files.`);
7179
return;
7280
}

lib/include.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module.exports = () => {
2626
return downloadIncludedFiles(values[1], null);
2727
});
2828

29-
const finished = downloaded.then((result) => {
29+
downloaded.then((result) => {
3030
process.stdout.write(`${result.successful.length} included file(s) were successfully updated`);
3131
displayCheckbox('green');
3232
if (result.failed.length > 0) {
@@ -36,10 +36,7 @@ module.exports = () => {
3636
process.stdout.write(` - ${fail}\n`);
3737
}
3838
}
39-
});
40-
41-
// Catch all the errors
42-
Promise.all([gotAuth, gotId, downloaded, checkedVersion, finished, ]).catch((err) => {
39+
}).catch((err) => {
4340
handleError(err, true);
4441
return;
4542
});

lib/init.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,10 @@ module.exports = (name) => {
2323
return remoteCreateProject(values[0], name);
2424
});
2525

26-
const finished = createdProject.then((result) => {
26+
createdProject.then((result) => {
2727
displayCheckbox('green');
2828
clone(result.id, name);
29-
});
30-
31-
// Catch all the errors
32-
Promise.all([gotAuth, createdProject, checkedVersion, finished, ]).catch((err) => {
29+
}).catch((err) => {
3330
handleError(err, true);
3431
return;
3532
});

lib/link.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,9 @@ module.exports = (identifier) => {
3232
return downloadRemote(values[1], metadata.id, null, 'link');
3333
});
3434

35-
const finished = downloaded.then(() => {
35+
downloaded.then(() => {
3636
displayCheckbox('green');
37-
});
38-
39-
// Catch all the errors
40-
Promise.all([gotAuth, gotMetadata, downloaded, checkedVersion, finished, ]).catch((err) => {
37+
}).catch((err) => {
4138
handleError(err, true);
4239
return;
4340
});

lib/list.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,10 @@ module.exports = (nameFilter) => {
1313
const checkedVersion = checkNewVersion();
1414
const gotAuth = authenticate([]);
1515

16-
const listed = Promise.all([gotAuth, checkedVersion, ]).then((values) => {
16+
Promise.all([gotAuth, checkedVersion, ]).then((values) => {
1717
return listScriptFiles(values[0], nameFilter, true, null, []);
18-
});
19-
20-
// Catch all the errors
21-
Promise.all([gotAuth, listed, checkedVersion, ]).catch((err) => {
22-
handleError(err, false);
18+
}).catch((err) => {
19+
handleError(err, true);
2320
return;
2421
});
2522
};

0 commit comments

Comments
 (0)