This repository has been archived by the owner on Nov 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
integrate.js
89 lines (74 loc) · 2.99 KB
/
integrate.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
/**
* Copyright 2015 Mozilla
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
var fs = require('fs');
var path = require('path');
var fse = require('fs-extra');
var cli = require('cli');
var chalk = require('chalk');
var spinnerMessage;
function startSpinner(message) {
spinnerMessage = message;
cli.spinner(' ' + message);
}
function stopSpinner(error) {
var symbol = error ? chalk.red.bold('✗ ') : chalk.green.bold('✓ ');
var result = error ? 'error' : 'done';
cli.spinner(symbol + spinnerMessage + ' ' + result + '!\n', true);
}
module.exports = function(config) {
return new Promise(function(resolve, reject) {
var dir = config.dir || './';
process.stdout.write('Integrating Oghliner into the app in the current directory…\n\n');
startSpinner('Copying offline-manager.js to ' + dir + '…');
fs.stat(dir, function(err, stat) {
if (err) {
reject(new Error(dir + ' doesn\'t exist or is not accessible.'));
return;
}
if (!stat.isDirectory()) {
reject(new Error(dir + ' isn\'t a directory.'));
return;
}
var dest = path.join(dir, 'offline-manager.js');
fse.copy(path.join(path.dirname(__dirname), 'templates', 'app', 'scripts', 'offline-manager.js'), dest, function(err) {
if (err) {
reject(err);
} else {
stopSpinner();
process.stdout.write(
'Oghliner has been integrated into the app!\n\n' +
'The app needs to load the offline-manager.js script in order to register\n' +
'the service worker that offlines the app. To load the script, add this line\n' +
'to the app\'s HTML page(s)/template(s):\n\n' +
chalk.bold('<script src="' + dest + '"></script>') + '\n\n' +
'Then commit the changes and push the commit to the origin/master branch:\n\n' +
chalk.bold('git commit -m"integrate Oghliner" --all') + '\n' +
chalk.bold('git push origin master') + '\n\n' +
'Then you can offline and deploy the app using the ' + chalk.bold.italic('offline') + ' and ' +
chalk.bold.italic('deploy') + ' commands.\n\n' +
chalk.blue.bold('ℹ For more information about offlining and deployment, see:\n' +
' https://mozilla.github.io/oghliner/') + '\n'
);
resolve();
}
});
});
}).catch(function(err) {
stopSpinner(err);
throw err;
});
};