forked from habitlab/habitlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.ls
1133 lines (999 loc) · 37.8 KB
/
gulpfile.ls
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require! {
'livescript'
'gulp'
'gulp-changed'
'gulp-util'
'gulp-print'
'gulp-livescript-async': 'gulp-livescript'
'gulp-yaml'
'gulp-eslint'
'gulp-header'
'path'
'fs'
'vinyl-named'
'webpack'
'gulp-vulcanize'
'gulp-crisper'
'del'
'deepcopy'
'js-yaml'
'mkdirp'
'glob'
'bestzip'
'chrome-web-store-item-property'
'livereload'
'semver'
'deepmerge'
}
is_debug_build = false
#UglifyJSPlugin = require 'uglifyjs-webpack-plugin'
BabiliPlugin = require 'babel-minify-webpack-plugin'
UglifyJsPlugin = require 'uglifyjs-webpack-plugin'
HabitLabComponentRenamePlugin = require './webpack_habitlab_component_rename_plugin'
HabitLabComponentListPlugin = require './webpack_habitlab_component_list_plugin'
fse = require 'fs-extra'
webpack-stream = require 'webpack4-stream-watch'
process.on 'unhandledRejection', (reason, p) ->
throw new Error(reason)
prelude = require 'prelude-ls'
gen_deps = require './scripts/generate_polymer_dependencies_lib.ls'
function_signatures = require './src/libs_common/function_signatures.ls'
webpack_config_frontend = require './webpack_config_frontend.ls'
webpack_config_backend = require './webpack_config_backend.ls'
lspattern = [
'src/redirect.ls'
'src/habitlab_website_redirect.ls'
'src/components/*.ls'
'src/fields/*.ls'
'src/libs_frontend/**/*.ls'
'src/libs_common/**/*.ls'
'src/libs_backend/**/*.ls'
'src/goals/**/*.ls'
]
lspattern_srcgen = [
'src/**/*.ls'
]
enabled_intervention_list = []
do ->
for info_yaml_filepath in glob.sync('src/interventions/**/*.yaml')
info_yaml_data = js-yaml.safeLoad fs.readFileSync info_yaml_filepath
if not info_yaml_data?
console.log 'syntax error in yaml file: ' + info_yaml_filepath
if not info_yaml_data.description?
console.log 'missing description: ' + info_yaml_filepath
continue
if info_yaml_data.disabled
console.log 'intervention disabled: ' + info_yaml_filepath
continue
basepath = info_yaml_filepath.substr(0, info_yaml_filepath.length - 10) # '/info.yaml'.length
basepath_parts = basepath.split('/')
basepath = basepath_parts[basepath_parts.length - 2 to].join('/')
enabled_intervention_list.push basepath
yamlpattern_manifest = [
'src/manifest.yaml'
'src/manifest_extra.yaml'
]
yamlpattern_base = [
'src/goals/**/*.yaml'
#'src/interventions/**/*.yaml'
'src/fields/**/*.yaml'
]
do ->
for enabled_intervention_name in enabled_intervention_list
yamlpattern_base.push 'src/interventions/' + enabled_intervention_name + '/*.yaml'
yamlpattern = yamlpattern_manifest.concat(yamlpattern_base)
eslintpattern = [
'src/**/*.js'
'src_gen/**/*.js'
#'src/components_skate/**/*.jsx'
#'src_gen/components_skate/**/*.js'
'!src/flowtypes/*.js'
'!src/bundles/*.js'
'!src_gen/bundles/*.js'
'!src/bugmuncher/*.js'
'!src_gen/bugmuncher/*.js'
'!src/bower_components/**/*.js'
'!src_gen/bower_components/**/*.js'
'!src/**/*.deps.js'
'!src/**/*.jspm.js'
'!src/jspm_packages/**/*.js'
'!src_gen/jspm_packages/**/*.js'
'!src/modules_custom/**/*.js'
'!src_gen/modules_custom/**/*.js'
]
jspattern_srcgen = [
'src/**/*.js'
'!src/flowtypes/*.js'
'!src/**/*.deps.js'
]
htmlpattern_srcgen = [
'src/**/*.html'
]
intervention_copypattern = []
do ->
for enabled_intervention_name in enabled_intervention_list
intervention_copypattern.push 'src_gen/interventions/' + enabled_intervention_name + '/**/*.js'
intervention_copypattern.push 'src_gen/interventions/' + enabled_intervention_name + '/*.js'
#intervention_copypattern = [
# 'src_gen/interventions/**/*.js'
#]
copypattern = [
'src/**/*.html'
'src/**/*.png'
'src/**/*.jpg'
'src/**/*.gif'
'src/**/*.svg'
'src/*.json'
'src/*.js'
'src/*.css'
'src/_locales/**/*'
'src/bugmuncher/*'
'src/bundles/*'
'src/libs_common/*.js'
'src/libs_backend/*.js'
'src/libs_frontend/*.js'
'src/goals/**/*.js'
'src/generated_libs/**/*.js'
'src/bower_components/**/*'
'src/components/**/*.js'
'src/modules_custom/**/*.js'
'src/modules_custom/**/*.css'
'!src/**/*.deps.js'
'!src/**/*.jspm.js'
'!src/jspm_packages/**/*'
]
do ->
for src_subfolder in ['goals', 'modules_custom', 'components']
for extension in ['html', 'png', 'jpg', 'gif', 'svg']
copypattern.push 'src/' + src_subfolder + '/**/*.' + extension
for enabled_intervention_name in enabled_intervention_list
for extension in ['html', 'png', 'jpg', 'gif', 'svg']
copypattern.push 'src/interventions/' + enabled_intervention_name + '/*.' + extension
copypattern.push 'src/interventions/' + enabled_intervention_name + '/**/*.' + extension
#copyjspmpattern = [
# 'src/components/**/*.jspm.js'
# 'src/components/*.jspm.js'
# 'src/bower_components/**/*.jspm.js'
#]
copyrootpattern = [
'jspm.config.js'
'jspm_config_frontend.js'
'jspm_config_backend.js'
]
webpack_pattern = [
'src/backend/background.ls'
'src/index.ls'
'src/index_jspm.ls'
'src/options.ls'
'src/popup.ls'
'src/index_loader.js'
]
webpack_pattern_content_scripts = [
#'src/interventions/**/*.ls'
#'src/interventions/**/*.js'
'src/intervention_utils/**/*.ls'
'src/intervention_utils/**/*.js'
'src/frontend_utils/**/*.ls'
'src/frontend_utils/**/*.js'
'src/frontend_utils/*.ls'
'src/frontend_utils/*.js'
#'src/components_skate/components_skate.js'
'!src/**/*.deps.js'
'!src/**/*.jspm.js'
]
do ->
for enabled_intervention_name in enabled_intervention_list
webpack_pattern_content_scripts.push 'src/interventions/' + enabled_intervention_name + '/*.ls'
webpack_pattern_content_scripts.push 'src/interventions/' + enabled_intervention_name + '/*.js'
webpack_vulcanize_pattern = [
'src_vulcanize/components/components.js'
]
vulcanize_html_pattern = [
'src_gen/components/components.html'
]
vulcanize_html_output_pattern = [
'src_vulcanize/components/components.html'
]
vulcanize_watch_pattern = [
'src/components/**/*.html'
'src/components/**/*.js'
'src/components/**/*.ls'
'!src/**/*.deps.js'
'!src/**/*.jspm.js'
]
eslint_config = js-yaml.safeLoad fs.readFileSync('.eslintrc.yaml', 'utf-8')
eslint_config.globals = Object.keys(eslint_config.globals)
gulp.task 'livescript_srcgen', ->
gulp.src(lspattern_srcgen, {base: 'src'})
.pipe(gulp-changed('src_gen', {extension: '.js'}))
#.pipe(gulp-print( -> "livescript_srcgen: #{it}" ))
.pipe(gulp-livescript({bare: true}))
.pipe(gulp-header('/* livescript */\n\n'))
.on('error', gulp-util.log)
.pipe(gulp.dest('src_gen'))
gulp.task 'js_srcgen', ->
return gulp.src(jspattern_srcgen, {base: 'src'})
.pipe(gulp-changed('src_gen'))
#.pipe(gulp-print( -> "js_srcgen: #{it}" ))
.pipe(gulp.dest('src_gen'))
gulp.task 'eslint', gulp.series gulp.parallel('livescript_srcgen', 'js_srcgen'), ->
gulp.src(eslintpattern, {base: 'src'})
#.pipe(gulp-print( -> "eslint_frontend: #{it}" ))
.pipe(gulp-eslint(eslint_config))
.pipe(gulp-eslint.formatEach('compact', process.stderr))
gulp.task 'livescript_build', ->
gulp.src(lspattern, {base: 'src'})
.pipe(gulp-changed('dist', {extension: '.js'}))
.pipe(gulp-print( -> "livescript: #{it}" ))
.pipe(gulp-livescript({bare: false}))
.on('error', gulp-util.log)
.pipe(gulp.dest('dist'))
empty_or_updated = (stream, cb, sourceFile, targetPath) ->
if not fs.existsSync(targetPath)
stream.push sourceFile
return cb!
if fs.statSync(targetPath).size == 0
stream.push sourceFile
return cb!
return gulp-changed.compareLastModifiedTime(stream, cb, sourceFile, targetPath)
fromcwd = (x) ->
path.join(process.cwd(), x)
run_gulp_webpack = (myconfig, options) ->
options ?= {}
{src_pattern, src_base} = options
src_pattern ?= webpack_pattern
src_base ?= 'src'
current_dir = process.cwd()
return gulp.src(src_pattern, {base: src_base})
#.pipe(gulp-changed('dist', {extension: '.js', hasChanged: empty_or_updated}))
.pipe(gulp-print( -> "webpack: #{it}" ))
.pipe(vinyl-named( (file) ->
relative_path = path.relative(path.join(current_dir, src_base), file.path)
relative_path_noext = relative_path.replace(/\.jsx$/, '').replace(/\.js$/, '').replace(/\.ls$/, '')
return relative_path_noext
))
#.pipe(webpack-stream(myconfig).on('error', gulp-util.log))
.pipe(webpack-stream(myconfig))
.on('error', (err) ->
gulp-util.log(err.message)
this.emit('end')
)
.pipe(gulp.dest('dist'))
with_created_object = (orig_obj, func_to_apply) ->
new_obj = deepcopy(orig_obj)
func_to_apply(new_obj)
return new_obj
webpack_config_watch = with_created_object webpack_config_backend, (o) ->
o.watch = true
o.devtool = false # comment out to generate source maps
webpack_config_nowatch = with_created_object webpack_config_backend, (o) ->
o.watch = false
o.devtool = false # comment out to generate source maps
webpack_config_watch_content_scripts = with_created_object webpack_config_frontend, (o) ->
o.watch = true
o.devtool = false # comment out to generate source maps
o.plugins.push new HabitLabComponentRenamePlugin()
webpack_config_nowatch_content_scripts = with_created_object webpack_config_frontend, (o) ->
o.watch = false
o.devtool = false # comment out to generate source maps
o.plugins.push new HabitLabComponentRenamePlugin()
webpack_config_nowatch_content_scripts_listcomponents = with_created_object webpack_config_frontend, (o) ->
o.watch = false
o.devtool = false # comment out to generate source maps
o.plugins.push new HabitLabComponentListPlugin()
webpack_config_nosrcmap_watch = with_created_object webpack_config_backend, (o) ->
o.watch = true
o.devtool = false
webpack_config_nosrcmap_nowatch = with_created_object webpack_config_backend, (o) ->
o.watch = false
o.devtool = false
webpack_config_prod_nowatch = with_created_object webpack_config_backend, (o) ->
#o.mode = 'production'
o.watch = false
o.devtool = false
o.plugins.push new webpack.LoaderOptionsPlugin {
debug: false
}
#o.plugins.push new HabitLabComponentRenamePlugin()
#o.plugins.push new BabiliPlugin {}, {
# comments: false
#}
o.plugins.push new UglifyJsPlugin({
uglifyOptions: {
ie8: false,
ecma: 8,
output: {
comments: false,
beautify: false
}
}
})
/*
if not o.optimization?
o.optimization = {}
o.optimization.minimizer = [
new UglifyJsPlugin({
uglifyOptions: {
ie8: false,
ecma: 8,
output: {
comments: false,
beautify: false
}
}
})
]
*/
# o.optimization.push {
# runtimeChunk: false,
# splitChunks: {
# cacheGroups: {
# default: false,
# commons: {
# test: /[\\/]node_modules[\\/]/,
# name: 'vendor_app',
# chunks: 'all',
# minChunks: 2
# }
# }
# }
# }
# o.plugins.push new UglifyJSPlugin {
# uglifyOptions: {
# ie8: false
# ecma: 8
# output: {
# comments: false
# beautify: false
# }
# }
# }
webpack_config_prod_nowatch_content_scripts = with_created_object webpack_config_frontend, (o) ->
#o.mode = 'production'
o.watch = false
o.devtool = false
o.plugins.push new webpack.LoaderOptionsPlugin {
debug: false
}
o.plugins.push new HabitLabComponentRenamePlugin()
#o.plugins.push new BabiliPlugin {}, {
# comments: false
#}
o.plugins.push new UglifyJsPlugin({
uglifyOptions: {
ie8: false,
ecma: 8,
output: {
comments: false,
beautify: false
}
}
})
/*
if not o.optimization?
o.optimization = {}
o.optimization.minimizer = [
new UglifyJsPlugin({
uglifyOptions: {
ie8: false,
ecma: 8,
output: {
comments: false,
beautify: false
}
}
})
]
*/
# o.plugins.push new UglifyJSPlugin {
# uglifyOptions: {
# ie8: false
# ecma: 8
# output: {
# comments: false
# beautify: false
# }
# }
# }
gulp.task 'webpack_vulcanize', ->
run_gulp_webpack webpack_config_nosrcmap_nowatch, {
src_pattern: webpack_vulcanize_pattern
src_base: 'src_vulcanize'
}
gulp.task 'webpack_vulcanize_watch', ->
run_gulp_webpack webpack_config_nosrcmap_watch, {
src_pattern: webpack_vulcanize_pattern
src_base: 'src_vulcanize'
}
gulp.task 'webpack_vulcanize_prod', ->
run_gulp_webpack webpack_config_prod_nowatch, {
src_pattern: webpack_vulcanize_pattern
src_base: 'src_vulcanize'
}
gulp.task 'yaml_build_base', ->
return gulp.src(yamlpattern_base, {base: 'src'})
.pipe(gulp-changed('dist', {extension: '.json'}))
.pipe(gulp-print( -> "yaml: #{it}" ))
.pipe(gulp-yaml({space: 2}))
.on('error', gulp-util.log)
.pipe(gulp.dest('dist'))
gulp.task 'yaml_build_manifest', (done) ->
manifest_file_contents = js-yaml.safeLoad fs.readFileSync('src/manifest.yaml')
if is_debug_build
manifest_file_contents.devtools_page = 'devtools.html'
if fs.existsSync 'src/manifest_extra.yaml'
manifest_file_extra = js-yaml.safeLoad fs.readFileSync 'src/manifest_extra.yaml'
manifest_file_contents = deepmerge(manifest_file_contents, manifest_file_extra)
fs.writeFileSync 'dist/manifest.json', JSON.stringify(manifest_file_contents, null, 2)
done()
gulp.task 'yaml_build', gulp.parallel('yaml_build_base', 'yaml_build_manifest')
gulp.task 'copy_interventions', gulp.series gulp.parallel('livescript_srcgen', 'js_srcgen'), ->
return gulp.src(intervention_copypattern, {root: 'src_gen/interventions'})
.pipe(gulp-changed('dist/intervention_templates'))
#.pipe(gulp-print( -> "copy: #{it}" ))
.pipe(gulp.dest('dist/intervention_templates'))
gulp.task 'copy_build', ->
return gulp.src(copypattern, {base: 'src'})
.pipe(gulp-changed('dist'))
#.pipe(gulp-print( -> "copy: #{it}" ))
.pipe(gulp.dest('dist'))
#gulp.task 'copy_jspm_build', ->
# console.log glob.sync('src/components/*.jspm.js')
# #console.log gulp.src(copyjspmpattern, {base: 'src'}).pipe(gulp-print( -> "copy_jspm_available_files: #{it}" ))
# return gulp.src(copyjspmpattern, {base: 'src'})
# #.pipe(gulp-changed('dist'))
# .pipe(gulp-print( -> "copy_jspm: #{it}" ))
# .pipe(gulp.dest('dist'))
gulp.task 'copy_root_build', ->
return gulp.src(copyrootpattern, {base: ''})
.pipe(gulp-changed('dist'))
.pipe(gulp.dest('dist'))
gulp.task 'generate_polymer_dependencies', (done) ->
gen_deps.set_src_path(path.join(process.cwd(), 'src'))
gen_deps.set_options()
gen_deps.generate_dependencies_for_all_files_in_src_path()
copy_file_pattern 'bower_components/**/*.deps.js', false
copy_file_pattern 'components/**/*.deps.js', true
done()
copy_file_patterns = (patterns, overwrite) ->
if not overwrite?
overwrite = false
for pattern in patterns
copy_file_pattern pattern, overwrite
return
copy_file_pattern = (pattern, overwrite) ->
if not overwrite?
overwrite = false
files_list = glob.sync path.join('src', pattern)
existing_files_list = glob.sync path.join('dist', pattern)
existing_files = {[x, true] for x in existing_files_list}
for src_file in files_list
dist_file = src_file.replace(/^src\//, 'dist/')
if not overwrite
if existing_files[dist_file]?
continue
fse.copySync src_file, dist_file
return
gulp.task 'generate_polymer_dependencies_jspm', (done) ->
gen_deps.set_src_path(path.join(process.cwd(), 'src'))
gen_deps.set_options({target_jspm: true})
gen_deps.generate_dependencies_for_all_files_in_src_path()
copy_file_pattern 'bower_components/**/*.jspm.js', false
copy_file_pattern 'components/**/*.jspm.js', true
done()
gulp.task 'generate_interventions_list', (done) ->
mkdirp.sync 'dist/interventions'
#if fs.existsSync('src/interventions/interventions.yaml')
# fs.unlinkSync('src/interventions/interventions.yaml')
output = []
for info_yaml_filepath in glob.sync('src/interventions/**/info.yaml')
intervention_info = js-yaml.safeLoad fs.readFileSync(info_yaml_filepath, 'utf-8')
if not intervention_info?
console.log "no intervention_info for intervention #{info_yaml_filepath}"
continue
if intervention_info.disabled
continue
intervention_name = info_yaml_filepath.replace(/^src\/interventions\//, '').replace(/\/info\.yaml$/, '')
output.push intervention_name
fs.writeFileSync 'dist/interventions/interventions.json', JSON.stringify(prelude.sort(output))
done()
gulp.task 'generate_goals_list', (done) ->
mkdirp.sync 'dist/goals'
#if fs.existsSync('src/goals/goals.yaml')
# fs.unlinkSync('src/goals/goals.yaml')
output = []
for info_yaml_filepath in glob.sync('src/goals/**/info.yaml')
goal_info = js-yaml.safeLoad fs.readFileSync(info_yaml_filepath, 'utf-8')
if not goal_info?
console.log "no goal_info for goal #{info_yaml_filepath}"
continue
if goal_info.disabled
continue
goal_name = info_yaml_filepath.replace(/^src\/goals\//, '').replace(/\/info\.yaml$/, '')
output.push goal_name
output.push 'debug/all_interventions'
fs.writeFileSync 'dist/goals/goals.json', JSON.stringify(prelude.sort(output))
done()
gulp.task 'generate_goal_intervention_info', (done) ->
interventions = []
goals = []
output = {interventions, goals}
intervention_name_to_info = {}
for info_yaml_filepath in glob.sync('src/interventions/**/info.yaml')
intervention_info = js-yaml.safeLoad fs.readFileSync(info_yaml_filepath, 'utf-8')
if not intervention_info?
console.log "no intervention_info for intervention #{info_yaml_filepath}"
continue
if intervention_info.disabled
continue
intervention_name = info_yaml_filepath.replace(/^src\/interventions\//, '').replace(/\/info\.yaml$/, '')
intervention_name_to_info[intervention_name] = intervention_info
goal_name_to_info = {}
for info_yaml_filepath in glob.sync('src/goals/**/info.yaml')
goal_info = js-yaml.safeLoad fs.readFileSync(info_yaml_filepath, 'utf-8')
if not goal_info?
console.log "no goal_info for goal #{info_yaml_filepath}"
continue
if goal_info.disabled
continue
goal_name = info_yaml_filepath.replace(/^src\/goals\//, '').replace(/\/info\.yaml$/, '')
goal_name_to_info[goal_name] = goal_info
# goals
for goal_name in prelude.sort(Object.keys(goal_name_to_info))
goal_info = goal_name_to_info[goal_name]
if not goal_info?
continue
goal_info.name = goal_name
if not goal_info.sitename?
goal_info.sitename = goal_name.split('/')[0]
if not goal_info.sitename_printable?
goal_info.sitename_printable = goal_info.sitename.substr(0, 1).toUpperCase() + goal_info.sitename.substr(1)
if not goal_info.homepage?
goal_info.homepage = "https://www.#{goal_info.sitename}.com/"
interventions_for_goal_new = []
if not goal_info.interventions?
console.log 'goal is missing interventions: ' + goal_name
continue
generic_intervention_categories = ['generic', 'generic_positive', 'video']
generic_intervention_name_to_specific_name = {}
for intervention_name in goal_info.interventions
generic_intervention_name_to_specific_name[intervention_name] = intervention_name
for generic_intervention_category in generic_intervention_categories
generic_intervention_category_with_slash = generic_intervention_category + '/'
if intervention_name.startsWith(generic_intervention_category_with_slash)
generic_intervention = intervention_name
make_absolute_path = (content_script) ->
if content_script.path?
if content_script.path[0] == '/'
return content_script
content_script.path = '/interventions/' + generic_intervention + '/' + content_script.path
return content_script
if content_script[0] == '/'
return content_script
return '/interventions/' + generic_intervention + '/' + content_script
intervention_info = JSON.parse JSON.stringify intervention_name_to_info[generic_intervention]
intervention_name = goal_info.sitename + '/' + generic_intervention.substr(generic_intervention_category_with_slash.length)
intervention_info.generic_intervention = generic_intervention
intervention_info.matches = [goal_info.domain]
if intervention_info.content_scripts?
intervention_info.content_scripts = intervention_info.content_scripts.map make_absolute_path
if intervention_info.background_scripts?
intervention_info.background_scripts = intervention_info.background_scripts.map make_absolute_path
intervention_name_to_info[intervention_name] = intervention_info
generic_intervention_name_to_specific_name[generic_intervention] = intervention_name
intervention_info = intervention_name_to_info[intervention_name]
if not intervention_info?
console.log 'error: goal ' + goal_info.name + ' lists nonexistent intervention ' + intervention_name
continue
intervention_info.goals = [goal_info.name]
interventions_for_goal_new.push intervention_name
goal_info.interventions = interventions_for_goal_new
if goal_info.default_interventions?
goal_info.default_interventions = goal_info.default_interventions.map(-> generic_intervention_name_to_specific_name[it])
else
goal_info.default_interventions = goal_info.interventions
goal_name_to_info[goal_name] = goal_info
goals.push goal_info
# interventions
for intervention_name in prelude.sort(Object.keys(intervention_name_to_info))
intervention_info = intervention_name_to_info[intervention_name]
intervention_info.name = intervention_name
generic_name = intervention_info.generic_intervention ? intervention_name
if not intervention_info.sitename?
intervention_info.sitename = intervention_name.split('/')[0]
if not intervention_info.sitename_printable?
intervention_info.sitename_printable = intervention_info.sitename.substr(0, 1).toUpperCase() + intervention_info.sitename.substr(1)
is_default = false
if intervention_info.goals?
for goal_name in intervention_info.goals
goal_info = goal_name_to_info[goal_name]
if goal_info.default_interventions? and goal_info.default_interventions.includes(intervention_name)
is_default = true
intervention_info.is_default = is_default
interventions.push intervention_info
fs.writeFileSync 'dist/goal_intervention_info.json', JSON.stringify(output)
done()
gulp.task 'generate_libs_frontend', (done) ->
mkdirp.sync 'src/generated_libs/libs_frontend'
for lib_name in prelude.sort(function_signatures.list_libs())
lib_contents = """// This file is generated from libs_common/function_signatures.ls
// Do not edit this file directly. To regenerate, run gulp
const {import_lib} = require('libs_frontend/import_lib');
module.exports = import_lib('#{lib_name}');
"""
fs.writeFileSync "src/generated_libs/libs_frontend/#{lib_name}.js", lib_contents
done()
gulp.task 'generate_expose_backend_libs', (done) ->
mkdirp.sync 'src/generated_libs/libs_backend'
output = []
output.push "const {expose_lib} = require('libs_backend/expose_lib');"
for lib_name in prelude.sort(function_signatures.list_libs())
output.push "expose_lib('#{lib_name}', require('libs_backend/#{lib_name}'));"
output.push ''
fs.writeFileSync "src/generated_libs/libs_backend/expose_backend_libs.js", output.join("\n")
done()
gulp.task 'generate_jspm_config_frontend', (done) ->
{get_alias_info} = require './alias_utils'
output = []
path_map = {}
for alias_info in get_alias_info()
{path, frontend} = alias_info
path_map[path] = frontend
for libname in fs.readdirSync 'src/modules_custom'
if libname.startsWith('.')
continue
package_json_file = "src/modules_custom/#{libname}/package.json"
package_json = JSON.parse fs.readFileSync(package_json_file, 'utf-8')
path_map[libname] = "modules_custom/#{libname}/#{package_json.main}"
fs.writeFileSync 'jspm_config_frontend.js', """
SystemJS.config({
map: #{JSON.stringify(path_map, null, 2)}
});
"""
done()
gulp.task 'generate_jspm_config_backend', (done) ->
{get_alias_info} = require './alias_utils'
output = []
path_map = {}
for alias_info in get_alias_info()
{path, backend} = alias_info
path_map[path] = backend
for libname in fs.readdirSync 'src/modules_custom'
if libname.startsWith('.')
continue
package_json_file = "src/modules_custom/#{libname}/package.json"
package_json = JSON.parse fs.readFileSync(package_json_file, 'utf-8')
path_map[libname] = "modules_custom/#{libname}/#{package_json.main}"
fs.writeFileSync 'jspm_config_backend.js', """
SystemJS.config({
map: #{JSON.stringify(path_map, null, 2)}
});
"""
done()
gulp.task 'generate_jspm_config', gulp.parallel('generate_jspm_config_frontend', 'generate_jspm_config_backend')
gulp.task 'generate_polymer_components_html', (done) ->
#if fs.existsSync('src/components/components.html')
# fs.unlinkSync('src/components/components.yaml')
output = []
output.push '<!-- This file is generated by gulp. Do not edit -->'
for filepath in prelude.sort(glob.sync('src/components/**/*.html'))
file_name = filepath.replace(/^src\/components\//, '')
if file_name == 'components.html'
continue
output.push "<link rel='import' href='#{file_name}'>"
output.push ''
fs.writeFileSync 'src/components/components.html', output.join("\n")
done()
extra_folders = ['modules_custom', 'intervention_templates', 'generated_libs', 'bundles', 'bower_components', 'libs_frontend', 'libs_common', 'libs_backend', 'components']
gulp.task 'make_extra_file_list', (done) ->
complete_file_list = []
for subfolder in extra_folders
for entry in glob.sync('dist/' + subfolder + '/**')
entry = entry.replace('dist/', '')
complete_file_list.push entry
fs.writeFileSync 'dist/extra_file_list.json', JSON.stringify(complete_file_list)
done()
gulp.task 'remove_extra_files', (done) ->
for subfolder in extra_folders
fse.removeSync 'dist/' + subfolder
done()
gulp.task 'generate_skate_components_js', (done) ->
#if fs.existsSync('src/components/components.html')
# fs.unlinkSync('src/components/components.yaml')
output = []
output.push '/* This file is generated by gulp. Do not edit */'
all_files = glob.sync('src/components_skate/**/*.jsx').concat glob.sync('src/components_skate/**/*.ls')
for filepath in prelude.sort(all_files)
file_name = filepath.replace(/^src\//, '').replace(/\.jsx$/, '').replace(/\.ls$/, '')
output.push "require('#{file_name}')"
output.push ''
fs.writeFileSync 'src/components_skate/components_skate.js', output.join("\n")
done()
gulp.task 'set_is_debug_build_true', (done) ->
is_debug_build := true
done()
gulp.task 'build_base', gulp.series('set_is_debug_build_true', gulp.parallel(
gulp.series('generate_polymer_components_html', 'generate_polymer_dependencies')
gulp.series('generate_jspm_config', 'copy_root_build')
#'generate_skate_components_js'
'generate_libs_frontend'
'generate_expose_backend_libs'
'generate_interventions_list'
'generate_goals_list'
'generate_goal_intervention_info'
'yaml_build'
'copy_build'
'livescript_build'
'copy_interventions'
), 'make_extra_file_list')
# based on
# https://github.com/webpack/webpack-with-common-libs/blob/master/gulpfile.js
# https://github.com/shama/webpack-stream
gulp.task 'webpack_build', ->
run_gulp_webpack webpack_config_nowatch
gulp.task 'webpack_watch', ->
run_gulp_webpack webpack_config_watch
gulp.task 'webpack_prod', ->
run_gulp_webpack webpack_config_prod_nowatch
gulp.task 'webpack_content_scripts', ->
run_gulp_webpack webpack_config_nowatch_content_scripts, {
src_pattern: webpack_pattern_content_scripts
}
gulp.task 'webpack_content_scripts_watch', ->
run_gulp_webpack webpack_config_watch_content_scripts, {
src_pattern: webpack_pattern_content_scripts
}
gulp.task 'webpack_content_scripts_prod', ->
run_gulp_webpack webpack_config_prod_nowatch_content_scripts, {
src_pattern: webpack_pattern_content_scripts
}
gulp.task 'html_srcgen', ->
return gulp.src(htmlpattern_srcgen, {base: 'src'})
.pipe(gulp-changed('src_gen'))
#.pipe(gulp-print( -> "html_srcgen: #{it}" ))
.pipe(gulp.dest('src_gen'))
gulp.task 'vulcanize', gulp.series gulp.parallel('livescript_srcgen', 'js_srcgen', 'html_srcgen'), ->
return gulp.src(vulcanize_html_pattern, {base: 'src_gen'})
.pipe(gulp-vulcanize({
#abspath: ''
#excludes: []
#stripExcludes: false
inlineScripts: true
inlineCss: false
}))
.pipe(gulp-crisper({
#scriptInHead: false
#onlySplit: false
#jsFileName: 'components.js'
}))
.pipe(gulp.dest('src_vulcanize'))
gulp.task 'copy_vulcanize', gulp.series 'vulcanize', ->
return gulp.src(vulcanize_html_output_pattern, {base: 'src_vulcanize'})
.pipe(gulp.dest('dist'))
/*
tasks_and_patterns = [
#['livescript_build', lspattern]
#['copy_vulcanize', vulcanize_watch_pattern]
#['typescript', tspattern]
#['es6', es6pattern]
['yaml', yamlpattern]
#['browserify_js', browserify_js_pattern]
#['browserify_ls', browserify_ls_pattern]
['copy', copypattern]
#['eslint', eslintpattern]
#['livescript_browserify', lspattern_browserify]
]
*/
gulp.task 'make_docs_markdown', (done) ->
{exec} = require 'shelljs'
if not fs.existsSync('doc')
fs.mkdirSync('doc')
exec('node ./node_modules/documentation-habitlab/bin/documentation.js build src_gen/libs_frontend/*.js src_gen/libs_backend/*.js src_gen/libs_common/*.js src/flowtypes/*.js -f md -o doc/API.md --github')
fse.copySync 'doc/API.md', 'dist/API.md'
done()
gulp.task 'make_docs', gulp.series('livescript_srcgen', 'js_srcgen', 'make_docs_markdown')
#gulp.task 'build', ['webpack', 'webpack_content_scripts', 'webpack_vulcanize']
gulp.task 'build', gulp.parallel('build_base', 'webpack_build', 'webpack_content_scripts')
gulp.task 'listcomponents', ->
run_gulp_webpack webpack_config_nowatch_content_scripts_listcomponents, {
src_pattern: webpack_pattern_content_scripts
}
gulp.task 'print_components_to_rename', (done) ->>
storage = require 'node-persist'
await storage.init()
components_list = await storage.keys()
console.log components_list
done()
gulp.task 'build_release', gulp.parallel(gulp.series('build_base', 'make_docs_markdown'), 'webpack_prod', 'webpack_content_scripts_prod')
mkzip_function = (actually_delete, suffix, done) ->
mkdirp.sync 'releases'
manifest_info = js-yaml.safeLoad(fs.readFileSync('src/manifest.yaml'))
version = manifest_info.version
output_zip_file = path.join('releases', "habitlab_#{version}.zip")
if suffix? and suffix.length > 0
output_zip_file = path.join('releases', "habitlab_#{version}_#{suffix}.zip")
if fs.existsSync('mkzip_tmp')
fse.removeSync('mkzip_tmp')
mkdirp.sync 'mkzip_tmp'
files_to_skip = [
'interventions/interventions.json'
'goals/goals.json'
'API.md'
'popup_loader.js'
'index_jspm.html'
'index_jspm.js'
'index_loader.js'
'index.js'
'systemjs_paths.js'
'logs.html'
'logs.js'
]
input_files = []
for filename in glob.sync('dist/**')
if not fs.lstatSync(filename).isFile()
continue
skip_file = false
x = filename.replace('dist/', '')
for extra_folder in extra_folders
if x.startsWith(extra_folder)
skip_file = true
if x.endsWith('/info.json') and (x.startsWith('goals/') or x.startsWith('interventions/'))
skip_file = true
if skip_file
continue
if files_to_skip.includes(x)
continue
input_files.push filename
for x in input_files
fse.ensureFileSync(x.replace('dist/', 'mkzip_tmp/'))
fse.copySync(x, x.replace('dist/', 'mkzip_tmp/'))
if not actually_delete
done()
return
<- bestzip output_zip_file, ['mkzip_tmp/*']
fse.removeSync('mkzip_tmp')
done()
return
gulp.task 'mkzip_fake', (done) ->
mkzip_function(false, null, done)
gulp.task 'mkzip', (done) ->
mkzip_function(true, null, done)
gulp.task 'mkzip_beta', (done) ->
mkzip_function(true, 'beta', done)
gulp.task 'mkzip_release', (done) ->
mkzip_function(true, 'release', done)
gulp.task 'buildmanifest_beta', (done) ->
manifest_file_contents = js-yaml.safeLoad fs.readFileSync('src/manifest.yaml')
if is_debug_build
manifest_file_contents.devtools_page = 'devtools.html'
if fs.existsSync 'src/manifest_extra_beta.yaml'
manifest_file_extra = js-yaml.safeLoad fs.readFileSync 'src/manifest_extra_beta.yaml'
manifest_file_contents = deepmerge(manifest_file_contents, manifest_file_extra)
fs.writeFileSync 'dist/manifest.json', JSON.stringify(manifest_file_contents, null, 2)
done()
gulp.task 'buildmanifest_release', (done) ->
manifest_file_contents = js-yaml.safeLoad fs.readFileSync('src/manifest.yaml')
if is_debug_build
manifest_file_contents.devtools_page = 'devtools.html'
if fs.existsSync 'src/manifest_extra_release.yaml'
manifest_file_extra = js-yaml.safeLoad fs.readFileSync 'src/manifest_extra_release.yaml'
manifest_file_contents = deepmerge(manifest_file_contents, manifest_file_extra)
fs.writeFileSync 'dist/manifest.json', JSON.stringify(manifest_file_contents, null, 2)
done()
gulp.task 'mkzip_beta', gulp.series('buildmanifest_beta', 'mkzip_beta')
gulp.task 'mkzip_release', gulp.series('buildmanifest_release', 'mkzip_release')
get_latest_published_version = ->>
latest_published_version = '0.0.0'
for extension_id in ['obghclocpdgcekcognpkblghkedcpdgd', 'bleifeoekkfhicamkpadfoclfhfmmina']
chrome_store_item = await chrome-web-store-item-property(extension_id)
published_version = chrome_store_item.version
if semver.gt(published_version, latest_published_version)
latest_published_version = published_version
return latest_published_version