-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathmanifestor.js
132 lines (116 loc) · 3.35 KB
/
manifestor.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
var _ = require('lodash');
var path = require('path');
var colors = require('colors');
var Promise = require('bluebird');
var fs = Promise.promisifyAll(require('fs'));
var google = require('googleapis');
var util = require('./util');
var defaults = require('./defaults');
var authenticate = require('./authenticate');
/**
build generates a manifest to be uploaded to google drive
@param externalFiles {Object} files in the cloud
@return {Object} manifest
*/
var build = function(externalFiles) {
return getConfig().get('path')
.then(util.getFilesFromDisk)
.then(function(files) {
// for each manifest file, if it has an equivalent on disk, keep it
// otherwise trash it
var filesToUpload = _.filter(externalFiles, function(externalFile) {
return util.hasFileOnDisk(files, externalFile);
});
_.each(files, function(file) {
// Add new file or update existing record
var manifestFile = getFileInManifest(filesToUpload, file);
if (manifestFile === undefined) {
// add
filesToUpload.push({
name: file.name,
type: util.getFileType(file),
source: file.content
});
} else {
// update
util.updateFileSource(manifestFile, file);
}
});
return filesToUpload;
});
};
function getFileInManifest(files, file) {
return _.findWhere(files, {
name: file.name,
type: util.getFileType(file)
});
}
function getExternalFiles(fileId) {
return authenticate()
.then(function(auth) {
return getProjectFiles(fileId, auth);
})
.catch(function(err) {
console.log('Script file ID not found. Please input an ID and try again.'.red);
throw err;
});
}
function getProjectFiles(fileId, auth) {
var drive = google.drive({ version: 'v3', auth });
var options = {
fileId,
mimeType: 'application/vnd.google-apps.script+json',
};
return new Promise(function(resolve, reject) {
drive.files.export(options, (err, res) => {
if (err) return reject(Error(err));
resolve(res);
})
})
.then(function(project) {
if (!project.files) {
throw 'Looks like there are no files associated with this project. Check the id and try again.';
}
return project.files;
})
.catch(SyntaxError, function(err) {
console.log('Error parsing project files'.red);
throw err;
})
.error(function(err){
throw err;
});
}
function throwIfConfig() {
return fs.readFileAsync(defaults.CONFIG_NAME)
.then(JSON.parse)
.then(function() {
throw 'Config already exists. Cowardly refusing to overwrite.';
})
.error(function() {
// swallow error
});
}
function getConfig() {
return fs.readFileAsync(defaults.CONFIG_NAME)
.then(JSON.parse)
.catch(SyntaxError, function(err) {
console.log('Error parsing config'.red);
throw err;
})
.error(function(err) {
console.log('Config does not exist'.red);
throw err;
});
}
function setConfig(config) {
return fs.writeFileAsync(defaults.CONFIG_NAME, JSON.stringify(config, "", 2))
.then(function() {
return config;
});
}
module.exports.build = build;
module.exports.get = getConfig;
module.exports.set = setConfig;
module.exports.getExternalFiles = getExternalFiles;
module.exports.throwIfConfig = throwIfConfig;