-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathfliplet-run.js
More file actions
179 lines (134 loc) · 4.44 KB
/
fliplet-run.js
File metadata and controls
179 lines (134 loc) · 4.44 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
[] Publish changes on file change
[] Should automatically run “npm run build/watch” behind the scenes if necessary and wait for that process to finish before it syncs the files
[] Be as fast as possible in making changes live (e.g. no recompilation)
[] Widget scoped to current studio user (package, name, orgId, etc)
[] Can only run on staging, development and local
[] Works on widgets, helpers, menus, themes
[] Should support multiple concurrent processes on different directories
*/
const _ = require('lodash');
const spawn = require('child_process').spawn;
const moment = require('moment');
const notifier = require('node-notifier');
const grunt = require('grunt');
const path = require('path');
const configstore = require('./lib/configstore');
const publish = require('./lib/publish');
const gruntFile = require('./lib/gruntfile');
const folderPath = process.cwd();
const widgetPackagePath = path.join(folderPath, 'widget.json');
const themePackagePath = path.join(folderPath, 'theme.json');
const menuPackagePath = path.join(folderPath, 'menu.json');
const npmPackagePath = path.join(folderPath, 'package.json');
const isWindows = process.platform === 'win32';
let buildProcess;
let widgetPackage;
let npmPackage;
grunt.log.header = function() {};
grunt.task.init = function() {};
function log(str) {
console.log(`${moment().format().magenta} ${str}`);
}
function error(str) {
console.error(`${moment().format().red} ${str.red.underline}`);
}
(async function() {
const user = publish.getUser();
if (!user) {
return console.error('Please log in first via the "fliplet login" command.');
}
log(`[USER] Logged in as ${user.fullName.green}. ${'Press Ctrl+C to exit at any time.'.blue}`);
try {
widgetPackage = require(widgetPackagePath);
log(`Loaded the ${widgetPackage.name.yellow} component.`);
} catch (e) {
try {
widgetPackage = require(themePackagePath);
log(`Loaded the ${widgetPackage.name.yellow} theme.`);
} catch (e) {
widgetPackage = require(menuPackagePath);
log(`Loaded the ${widgetPackage.name.yellow} menu.`);
}
}
try {
npmPackage = require(npmPackagePath);
log('A npm package file has been loaded from disk.');
} catch (e) {
log('A npm package file has not been found'.yellow);
}
const hasNpmScript = !!_.get(npmPackage, 'scripts.watch');
let gruntFinished;
let npmFinished = !hasNpmScript;
const gruntCallback = function() {
gruntFinished = true;
if (npmFinished) {
publishWidget();
}
};
gruntFile(grunt, {
callback: gruntCallback,
hasNpmScript
});
grunt.tasks(['default']);
if (hasNpmScript) {
log('Building source code...');
buildProcess = spawn(isWindows ? 'npm.cmd' : 'npm', ['run', 'watch'], { cwd: folderPath });
let buildFinishedLookup = 'Finished \'build\'';
buildProcess.stdout.setEncoding('utf8');
buildProcess.stdout.on('data', function(data) {
data = data.toString().trim();
if (!data) {
return;
}
// Uncomment for debugging
// log(`${'[BUILD]'.green} ${data}`);
if (npmFinished && data.indexOf('Starting') !== -1) {
log(`${'[WATCH]'.green} File changes have been detected to the source code.`);
}
if (data.indexOf(buildFinishedLookup) !== -1) {
npmFinished = true;
buildFinishedLookup = 'Finished';
if (gruntFinished) {
publishWidget();
}
}
});
buildProcess.stderr.setEncoding('utf8');
buildProcess.stderr.on('data', function(data) {
error(data.toString());
});
}
})();
let enqueuePublishing;
let isPublishing;
function publishWidget() {
if (isPublishing) {
enqueuePublishing = true;
return Promise.resolve();
}
log(`${'[SYNC]'.yellow} Your changes are being synchronized to Fliplet Studio...`);
return publish.run(null, {
logger: log,
publishingOptions: {
currentRegionOnly: 'true',
skipRecompilation: 'true'
}
})
.then(function(response) {
if (enqueuePublishing) {
enqueuePublishing = false;
return publishWidget();
}
notifier.notify({
title: 'Your changes are live on Fliplet Studio',
message: `${response.widget.name} (${response.widget.version}) is up to date on ${configstore.get('env')}.`,
icon: path.join(folderPath, 'assets', 'logo.png'),
sound: false
});
})
.catch(function(error) {
error(error);
process.exit();
});
}