/
unpackRemote.js
executable file
路106 lines (90 loc) 路 3.77 KB
/
unpackRemote.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const fs = require('fs-extra');
const path = require('path');
const createFile = require('./createFile.js');
const getAllFiles = require('./getAllFiles.js');
const getAllFolders = require('./getAllFolders.js');
const eaft = require('./extensionAndFiletype.js');
const constants = require('../constants.js');
const ignore = require('./ignore.js');
/**
* Unpack a remote google script file into seperate .js and .html files
*
* @param {String} rootFolder - relative path to the rootFolder of the project
* @param {String} fileName - if specified, only this file will get unpacked
* @returns {Promise} - A promise resolving no value
*/
function unpackRemote(rootFolder, fileName) {
let foundSingleFile = false;
const local = path.join(rootFolder, constants.META_DIR, constants.META_LOCAL);
const remote = path.join(rootFolder, constants.META_DIR, constants.META_REMOTE);
// Read local files
const localFiles = getAllFiles(rootFolder, '.', []);
// Get valid extensions for code files
const extensions = eaft.getCodeExtensions();
// Get contents of .gasignore file
const ignoreRegexes = eaft.getIgnoreRegexes(rootFolder);
// Read remote.json
const data = fs.readFileSync(remote, 'utf8');
const result = JSON.parse(data);
// Create all javascript/html files from remote.json
const remoteFiles = [];
const remoteNames = [];
for (const file of result.files) {
const extension = eaft.getExtensionFromFiletype(file.type, extensions);
const remoteFileName = file.name + extension;
file.name = path.join(rootFolder, remoteFileName);
remoteNames.push(remoteFileName);
const included = file.name.substring(0, constants.INCLUDE_DIR.length + 1) === `${constants.INCLUDE_DIR}/`;
// What files do we need to create?
if (!fileName) {
remoteFiles.push(file);
} else if (fileName === remoteFileName) {
remoteFiles.push(file);
foundSingleFile = true;
}
}
// If we have not found our file
if (fileName && !foundSingleFile) {
console.log(`Can't seem to find the file '${fileName}' in this project`);
return;
}
// Write local.json
createFile({
name: local,
source: data,
});
// Sync create all necessary files
for (const remoteFile of remoteFiles) {
createFile(remoteFile);
}
// If there was no file specified to pull we will do a cleanup
if (!fileName) {
// Remove all .gs, .js, .html and appsscript.json files that were not in remote.json unless they are in .gasignore
const toDelete = [];
for (const localFileName of localFiles) {
const localExtension = path.parse(localFileName).ext;
const fileNameWithoutExtension = path.join(path.parse(localFileName).dir, path.parse(localFileName).name);
if (eaft.isPushable(localExtension, fileNameWithoutExtension, extensions, ignoreRegexes) &&
!remoteNames.includes(localFileName) && localFileName !== constants.INCLUDE_FILE) {
toDelete.push(path.join(rootFolder, localFileName));
}
}
// Delete files
for (const fileToDelete of toDelete) {
fs.removeSync(fileToDelete);
}
// Remove all empty folders
const allFolders = getAllFolders(rootFolder).sort().reverse();
for (const emptyFolder of allFolders) {
const files = fs.readdirSync(emptyFolder);
if (files.length === 0) {
fs.removeSync(emptyFolder);
} else if (files.length === 1 && files[0] === '.DS_Store') {
fs.removeSync(emptyFolder);
}
}
}
ignore.addGitIgnore(rootFolder);
ignore.addGasIgnore(rootFolder);
}
module.exports = unpackRemote;