-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathGruntfile.js
201 lines (190 loc) · 6.69 KB
/
Gruntfile.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// This Gruntfile generates `publish` tasks,
// that upload a directory of static assets
// to a directory in the demos.algorithmia.com bucket
//
// Note: this doesn't currently do any asset path adjustments
// if the demo uses assets or links that start with '/',
// they probably won't load as expected
module.exports = function(grunt) {
// These are the demos that we generate publish tasks for.
// slug: generates a publish-<slug> task that publishes to demos.algorithmia.com/<slug>
// dist: the directory containing a static files to publish (index.html should be in this directory)
var demos = [
{ slug: 'amazon-miner', dist: 'JavaScript/amazon-miner'},
{ slug: 'autotag', dist: 'JavaScript/autotag' },
{ slug: 'analyze-tweets', dist: 'JavaScript/analyze-tweets'},
{ slug: 'classify-places', dist: 'JavaScript/places-demo'},
{ slug: 'cluster', dist: 'JavaScript/cluster' },
{ slug: 'colorize-photos', dist: 'JavaScript/colorization-demo' },
{ slug: 'doc-classifier', dist: 'JavaScript/doc-classifier'},
{ slug: 'deep-fashion', dist: 'JavaScript/deep-fashion'},
{ slug: 'deep-style', dist: 'JavaScript/deep-filter'},
{ slug: 'flow', dist: 'JavaScript/flow' },
{ slug: 'github', dist: 'JavaScript/github' },
{ slug: 'hacker-news', dist: 'JavaScript/hacker-news' },
{ slug: 'handwriting', dist: 'JavaScript/handwriting' },
{ slug: 'image-tagger', dist: 'JavaScript/image-tagger' },
{ slug: 'isitnude', dist: 'JavaScript/isitnude'},
{ slug: 'json', dist: 'JavaScript/json' },
{ slug: 'lda', dist: 'JavaScript/lda' },
{ slug: 'lda2', dist: 'JavaScript/lda2' },
{ slug: 'othello', dist: 'JavaScript/othello' },
{ slug: 'pathplan', dist: 'JavaScript/pathplan' },
{ slug: 'rss-dashboard', dist: 'JavaScript/RSS_dashboard'},
{ slug: 'sitemap', dist: 'JavaScript/site-mapper'},
{ slug: 'social-image-rec', dist: 'JavaScript/social-image-rec'},
{ slug: 'timeseries', dist: 'JavaScript/time-series'},
{ slug: 'twitter-ngram', dist: 'JavaScript/twitter-ngram' },
{ slug: 'valentines', dist: 'JavaScript/valentines' },
{ slug: 'video-metadata', dist: 'JavaScript/video-metadata'},
{ slug: 'video-search', dist: 'JavaScript/video-search'},
{ slug: 'video-toolbox', dist: 'JavaScript/video-toolbox'},
{ slug: 'web-page-inspector', dist: 'JavaScript/web-page-inspector'},
];
var awsS3Config = {
options: {
accessKeyId: '<%= aws.key %>',
secretAccessKey: '<%= aws.secret %>',
sessionToken: '<%= aws.session %>',
bucket: 'demos.algorithmia.com',
region: 'us-east-1',
uploadConcurrency: 5,
differential: true,
}
};
var cloudfrontConfig = {
options: {
accessKeyId: '<%= aws.key %>',
secretAccessKey: '<%= aws.secret %>',
distributionId: 'E17KLXK0RTG18V',
path: "/*"
}
};
var cleanConfig = {};
var copyConfig = {};
var templateConfig = {};
var watchConfig = {
templates: {
files: ['JavaScript/common/**/*','Gruntfile.js'],
options: { reload: true }
}
};
const getTemplate = (template, slug) =>
grunt.file
.read(template)
.replace(new RegExp("<%= slug %>", "g"), slug)
.split("[[VERSION]]")
.join(new Date().getTime());
const isIndexHTML = filepath => /index\.html$/.test(filepath);
demos.forEach(function(demo) {
const file = {
expand: true,
cwd: "build/" + demo.slug,
src: ["**"],
dest: demo.slug,
filter: filepath => !isIndexHTML(filepath)
};
awsS3Config[demo.slug] = {
files: [
// Non index.html files are uploaded to S3 directly.
file,
// index.html files are given metadata value to enable 301 redirect
// e.g., site.com/foo/index.html and site.com/foo/ will both redirect
// to site.com/foo
{
...file,
filter: isIndexHTML,
params: {
WebsiteRedirectLocation: `/${demo.slug}`
}
},
// Upload the equivalent of index.html, but without an extension and
// renamed to the demo.slug value so that the 301 doesn't 404.
{
...file,
filter: isIndexHTML,
dest: "/",
rename: () => demo.slug
}
]
};
cleanConfig[demo.slug] = ['build/'+demo.slug];
copyConfig[demo.slug] = {
files: [
{
expand: true,
cwd: 'JavaScript/common',
src: ['css/**','fonts/**','images/**','js/**'],
dest: 'build/'+demo.slug+'/public/'
},
{
expand: true,
cwd: demo.dist,
src: ['**/*'],
dest: 'build/'+demo.slug
}
]
};
cloudfrontConfig[demo.slug] = {};
templateConfig[demo.slug] = {
options: {
data: {
slug: demo.slug,
header_begin: getTemplate('JavaScript/common/header_begin.html', demo.slug),
header_end: getTemplate('JavaScript/common/header_end.html', demo.slug),
footer: getTemplate('JavaScript/common/footer.html', demo.slug),
ga_script: getTemplate('JavaScript/common/ga_script.html', demo.slug),
segment_script: getTemplate('JavaScript/common/segment_script.html', demo.slug)
}
},
files: [
{
expand: true,
cwd: demo.dist,
src: ['**/*.js','**/*.css','**/*.htm*'],
dest: 'build/'+demo.slug,
},
]
};
watchConfig[demo.slug] = {
files: [demo.dist+'/**/*.htm*', demo.dist+'/**/*.css', demo.dist+'/**/*.js', demo.dist+'/**/*.md', demo.dist+'/**/*.png', demo.dist+'/**/*.jpg'],
tasks: ['copy:' + demo.slug, 'template:' + demo.slug],
options: {
spawn: false
},
};
});
grunt.initConfig({
aws: grunt.file.readJSON('aws-keys.json'),
aws_s3: awsS3Config,
cloudfront_invalidate: cloudfrontConfig,
clean: cleanConfig,
copy: copyConfig,
template: templateConfig,
watch: watchConfig
});
grunt.loadNpmTasks('grunt-aws-s3');
grunt.loadNpmTasks('grunt-cloudfront-invalidate');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-template');
demos.forEach(function(demo) {
grunt.registerTask(
"publish:" + demo.slug,
"Publish the " + demo.slug + " demo",
[
"clean:" + demo.slug,
"copy:" + demo.slug,
"template:" + demo.slug,
"aws_s3:" + demo.slug,
"cloudfront_invalidate:" + demo.slug
]
);
grunt.registerTask(
"build:" + demo.slug,
"Build the " + demo.slug + " demo",
["clean:" + demo.slug, "copy:" + demo.slug, "template:" + demo.slug]
);
});
};