-
Notifications
You must be signed in to change notification settings - Fork 13
/
gulpfile.js
135 lines (112 loc) · 4.04 KB
/
gulpfile.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
133
134
135
'use strict';
var babelify = require('babelify')
var browserify = require('browserify')
var copy = require('recursive-copy')
var del = require('del')
var expandTilde = require('expand-tilde')
var fs = require('fs')
var fse = require('fs-extra')
var gulp = require('gulp')
var path = require('path')
var runSequence = require('run-sequence')
var source = require('vinyl-source-stream')
var uglify = require("gulp-uglify")
var SKETCH_PLUGINS_FOLDER = path.join(expandTilde('~'), '/Library/Application Support/com.bohemiancoding.sketch3/Plugins');
var ManifestProcessorOptions = {
pluginManifestDescriberName: 'SketchPlugin',
startingManifestTag: '__$begin_of_manifest_\n',
endingManifestTag: '__$end_of_manifest_\n',
scriptFileName: 'plugin.js',
globalVarName: '__globals'
};
var currentManifest = {};
function extractManifestObject() {
var data = fs.readFileSync(path.join(__dirname, 'build', ManifestProcessorOptions.scriptFileName), 'utf8');
var startTag = ManifestProcessorOptions.startingManifestTag;
var endTag = ManifestProcessorOptions.endingManifestTag;
var startIndex = data.indexOf(startTag);
var endIndex = data.indexOf(endTag);
if (startIndex === -1 || endIndex === -1) {
return;
}
return JSON.parse(data.substring(startIndex + startTag.length, endIndex));
}
function normalizePluginFileName() {
return currentManifest.name + '.sketchplugin';
}
gulp.task('clean', function () {
return del(['build', 'dist']);
});
gulp.task('prepare-manifest', function (callback) {
var manifest = extractManifestObject();
fse.outputJsonSync(path.join(__dirname, 'build/manifest.json'), manifest);
currentManifest = manifest;
callback(null);
});
gulp.task('prepare-folders', function (callback) {
if (!fs.existsSync(path.join(__dirname, 'build'))) {
fs.mkdirSync(path.join(__dirname, 'build'))
}
if (!fs.existsSync(path.join(__dirname, 'dist'))) {
fs.mkdirSync(path.join(__dirname, 'dist'))
}
callback()
});
gulp.task('assemble-plugin-bundle', function (callback) {
var bundlePath = path.join(__dirname, 'dist', normalizePluginFileName());
var script = fs.readFileSync(path.join(__dirname, 'build', ManifestProcessorOptions.scriptFileName), 'utf8');
script = ["var " + ManifestProcessorOptions.globalVarName + " = this;", script].join("");
fse.outputJson(path.join(bundlePath, 'Contents', 'Sketch', 'manifest.json'), currentManifest, function(){
fse.outputFile(path.join(bundlePath, 'Contents', 'Sketch', ManifestProcessorOptions.scriptFileName), script, callback)
})
});
gulp.task('install-plugin', function (callback) {
copy('dist', SKETCH_PLUGINS_FOLDER, { overwrite: true }, function(error, results){
console.log('Plugin installed')
callback()
})
});
gulp.task('build', function (callback) {
runSequence('clean', 'prepare-folders', 'bundle', 'prepare-manifest', 'uglify', 'assemble-plugin-bundle', 'install-plugin', callback);
});
gulp.task('bundle', function () {
var filePath = './src/plugin.js';
var extensions = ['.js'];
var bundler = browserify({
entries: [filePath],
extensions: extensions,
debug: false
});
bundler.transform("babelify",{
global: true,
presets: ["es2015"],
plugins: [
"transform-es2015-constants",
"transform-es2015-block-scoping",
"transform-es2015-arrow-functions",
"transform-es2015-block-scoped-functions",
["babel-plugin-sketch-manifest-processor", ManifestProcessorOptions]
]
});
return bundler.bundle()
.pipe(source(ManifestProcessorOptions.scriptFileName))
.pipe(gulp.dest('./build/'))
});
gulp.task('uglify', function() {
return gulp.src('build/*.js')
.pipe(uglify())
.pipe(gulp.dest('build/'))
})
gulp.task('watch', function () {
runSequence('build', function () {
gulp.watch('./src/**/*.*', function () {
console.log("Watching...");
runSequence('clean', 'build', function () {
console.log("Rebuild complete!");
});
});
});
});
gulp.task('default', function (callback) {
runSequence('build', callback);
});