-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup-sda.mjs
executable file
·2878 lines (2452 loc) · 75.6 KB
/
setup-sda.mjs
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
#!/usr/bin/env node
import { execSync } from "node:child_process";
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
import process from "node:process";
console.log("\nStarting sda setup...");
console.log("\n1 - Checking runtime and options...");
let runtime;
console.log(` => Your navigator userAgent is: ${navigator.userAgent}`);
const userAgent = navigator.userAgent.toLocaleLowerCase();
if (userAgent.includes("bun")) {
runtime = "bun";
} else if (userAgent.includes("deno")) {
runtime = "deno";
} else if (userAgent.includes("node")) {
runtime = "nodejs";
} else {
throw new Error("Unknown runtime.");
}
const args = process.argv.slice(2);
if (args.includes("--git")) {
console.log(` => You passed the option --git`);
}
if (args.includes("--framework")) {
console.log(` => You passed the option --framework`);
const readme =
`This repository has been created with [setup-sda](https://github.com/nshiab/setup-sda/).
It's using [simple-data-analysis](https://github.com/nshiab/simple-data-analysis), [journalism](https://github.com/nshiab/journalism) and [Framework](https://github.com/observablehq/framework) with [Plot](https://github.com/observablehq/plot).
Here's the recommended workflow:
- Put your raw data in the \`sda/data-raw\` folder. Note that this folder is gitignored.
- Use the \`sda/main.ts\` file to clean and process your raw data. Write the results to the \`src/data\` folder.
- Import your processed data from the \`src/data\` folder into the \`src/index.md\` and use it for your content.
When working on your project, you can use the following commands:
- \`npm run sda\` will watch your \`sda/main.ts\` and its dependencies. Everytime you'll save some changes, the data will be reprocessed.
- \`npm run dev\` will start a local server and watch all \`src/*.md\` files and their dependencies. Everytime you'll save some changes or the data is reprocessed, the content will be updated.
By opening two terminals each running one of the above commands, you'll be able to work on your project with a live preview of your content and data.
`;
const packageJson = {
type: "module",
scripts: {
clean:
"rimraf src/.observablehq/cache && rimraf .sda-cache && rimraf .temp",
build: "observable build",
dev: "observable preview",
deploy: "observable deploy",
observable: "observable",
sda: "node --experimental-strip-types --no-warnings --watch sda/main.ts",
},
};
const denoJson = {
tasks: {
clean:
"rimraf src/.observablehq/cache && rimraf .sda-cache && rimraf .temp",
build: "observable build",
dev: "observable preview",
deploy: "observable deploy",
observable: "observable",
sda: "deno run --node-modules-dir=auto -A --watch sda/main.ts",
},
nodeModulesDir: "auto",
};
const observableConfigJS = `
// See https://observablehq.com/framework/config for documentation.
export default {
// The app’s title; used in the sidebar and webpage titles.
title: "My new project",
// The pages and sections in the sidebar. If you don’t specify this option,
// all pages will be listed in alphabetical order. Listing pages explicitly
// lets you organize them into sections and have unlisted pages.
// pages: [
// {
// name: "Examples",
// pages: [
// {name: "Dashboard", path: "/example-dashboard"},
// {name: "Report", path: "/example-report"}
// ]
// }
// ],
// Content to add to the head of the page, e.g. for a favicon:
// head: '<link rel="icon" href="observable.png" type="image/png" sizes="32x32">',
// The path to the source root.
root: "src",
// Some additional configuration options and their defaults:
theme: "light", // try "light", "dark", "slate", etc.
// header: "", // what to show in the header (HTML)
// footer: "Built with Observable.", // what to show in the footer (HTML)
// sidebar: true, // whether to show the sidebar
toc: true, // whether to show the table of contents
// pager: true, // whether to show previous & next links in the footer
// output: "dist", // path to the output root for build
// search: true, // activate search
// linkify: true, // convert URLs in Markdown to links
// typographer: false, // smart quotes and other typographic improvements
// cleanUrls: true, // drop .html from URLs
};
`;
const data = `city,decade,meanTemp
Toronto,1840.0,7.1
Toronto,1850.0,6.4
Toronto,1860.0,6.9
Toronto,1870.0,6.8
Montreal,1880.0,5.3
Toronto,1880.0,6.6
Montreal,1890.0,6.0
Toronto,1890.0,7.6
Montreal,1900.0,5.8
Toronto,1900.0,7.7
Montreal,1910.0,6.1
Toronto,1910.0,8.1
Montreal,1920.0,6.2
Toronto,1920.0,8.0
Montreal,1930.0,6.8
Toronto,1930.0,8.6
Montreal,1940.0,6.9
Toronto,1940.0,8.7
Montreal,1950.0,7.4
Toronto,1950.0,9.2
Montreal,1960.0,7.4
Toronto,1960.0,8.8
Montreal,1970.0,7.3
Toronto,1970.0,9.0
Montreal,1980.0,7.7
Toronto,1980.0,9.0
Toronto,1990.0,9.6
Montreal,2000.0,7.6
Toronto,2000.0,9.7
Montreal,2010.0,8.0
Toronto,2010.0,9.9
`;
const tempJSON = `[
{"city":"Toronto","decade":1840.0,"meanTemp":7.1},
{"city":"Toronto","decade":1850.0,"meanTemp":6.4},
{"city":"Toronto","decade":1860.0,"meanTemp":6.9},
{"city":"Toronto","decade":1870.0,"meanTemp":6.8},
{"city":"Montreal","decade":1880.0,"meanTemp":5.3},
{"city":"Toronto","decade":1880.0,"meanTemp":6.6},
{"city":"Montreal","decade":1890.0,"meanTemp":6.0},
{"city":"Toronto","decade":1890.0,"meanTemp":7.6},
{"city":"Montreal","decade":1900.0,"meanTemp":5.8},
{"city":"Toronto","decade":1900.0,"meanTemp":7.7},
{"city":"Montreal","decade":1910.0,"meanTemp":6.1},
{"city":"Toronto","decade":1910.0,"meanTemp":8.1},
{"city":"Montreal","decade":1920.0,"meanTemp":6.2},
{"city":"Toronto","decade":1920.0,"meanTemp":8.0},
{"city":"Montreal","decade":1930.0,"meanTemp":6.8},
{"city":"Toronto","decade":1930.0,"meanTemp":8.6},
{"city":"Montreal","decade":1940.0,"meanTemp":6.9},
{"city":"Toronto","decade":1940.0,"meanTemp":8.7},
{"city":"Montreal","decade":1950.0,"meanTemp":7.4},
{"city":"Toronto","decade":1950.0,"meanTemp":9.2},
{"city":"Montreal","decade":1960.0,"meanTemp":7.4},
{"city":"Toronto","decade":1960.0,"meanTemp":8.8},
{"city":"Montreal","decade":1970.0,"meanTemp":7.3},
{"city":"Toronto","decade":1970.0,"meanTemp":9.0},
{"city":"Montreal","decade":1980.0,"meanTemp":7.7},
{"city":"Toronto","decade":1980.0,"meanTemp":9.0},
{"city":"Toronto","decade":1990.0,"meanTemp":9.6},
{"city":"Montreal","decade":2000.0,"meanTemp":7.6},
{"city":"Toronto","decade":2000.0,"meanTemp":9.7},
{"city":"Montreal","decade":2010.0,"meanTemp":8.0},
{"city":"Toronto","decade":2010.0,"meanTemp":9.9}
]`;
const tempRegressionsJSON = `[
{"city":"Montreal","x":"decade","y":"meanTemp","slope":0.0196,"yIntercept":-31.3115,"r2":0.921},
{"city":"Toronto","x":"decade","y":"meanTemp","slope":0.0203,"yIntercept":-30.7911,"r2":0.9261}
]`;
const mainTs = `import { SimpleDB } from "@nshiab/simple-data-analysis";
import crunchData from "./functions/crunchData.ts";
const sdb = new SimpleDB({ logDuration: true });
await crunchData(sdb);
await sdb.done();
`;
const crunchDataTs =
`import type { SimpleDB } from "@nshiab/simple-data-analysis";
export default async function crunchData(sdb: SimpleDB) {
// The mean temperature per decade.
const temp = sdb.newTable("temp");
await temp.loadData("sda/data-raw/temp.csv");
await temp.logTable();
// We compute a linear regression for each city.
const tempRegressions = await temp.linearRegressions({
x: "decade",
y: "meanTemp",
categories: "city",
decimals: 4,
outputTable: "tempRegressions",
});
await tempRegressions.logTable();
// We write the results to src/data.
await temp.writeData("src/data/temp.json");
await tempRegressions.writeData("src/data/temp-regressions.json");
}
`;
const indexMd = `\`\`\`ts
// To import .ts files, you need to use the .js extension
import getTempChange from "./helpers/getTempChange.js";
import createChart from "./components/createChart.js";
const temp = FileAttachment("data/temp.json").json();
const tempRegressions = FileAttachment("data/temp-regressions.json").json();
\`\`\`
# My new project
This is the home page of your new project.
The content below is just an example.
## Climate change
\`\`\`ts
const cities = tempRegressions.map((d) => d.city);
const city = view(Inputs.select(cities, { label: "Pick a city" }));
\`\`\`
On average, the temperature in <b>\${city}</b> has increased by \${getTempChange(
city,
tempRegressions
)} per decade.
\`\`\`ts
// Width is directly available and reactive. The chart will be redrawn when the width changes.
display(createChart(city, temp, tempRegressions, width));
\`\`\`
Here's all the data.
\`\`\`ts
const cityTemp = temp.filter((d) => d.city === city);
display(Inputs.table(cityTemp));
\`\`\`
## Methodology
This project uses a simple linear regression to estimate the temperature change in each city. The data comes from the Adjusted and Homogenized Canadian Climate Data (AHCCD) dataset.
To compute the regressions, we used the [simple-data-analysis](https://github.com/nshiab/simple-data-analysis) library.
\`\`\`ts run=false
import type { SimpleDB } from "@nshiab/simple-data-analysis";
export default async function crunchData(sdb: SimpleDB) {
// The mean temperature per decade.
const temp = sdb.newTable("temp");
await temp.loadData("sda/data-raw/temp.csv");
await temp.logTable();
// We compute a linear regression for each city.
const tempRegressions = await temp.linearRegressions({
x: "decade",
y: "meanTemp",
categories: "city",
decimals: 4,
outputTable: "tempRegressions",
});
await tempRegressions.logTable();
// We write the results to src/data.
await temp.writeData("src/data/temp.json");
await tempRegressions.writeData("src/data/temp-regressions.json");
}
\`\`\`
`;
const createChartTs = `
import { dot, line, plot } from "npm:@observablehq/plot";
export default function createChart(
city: string,
temp: { city: string; decade: number; meanTemp: number }[],
tempRegressions: {
city: string;
slope: number;
yIntercept: number;
r2: number;
}[],
width: number
) {
const tempCity = temp.filter((t) => t.city === city);
// We create x and y coordinates to draw a line based on the linear regression.
const regressionCity = tempRegressions.find((r) => r.city === city);
if (regressionCity === undefined) {
throw new Error(\`City \${city} not found in tempRegressions.\`);
}
const x1 = tempCity[0].decade;
const x2 = tempCity[tempCity.length - 1].decade;
const y1 = regressionCity.yIntercept + x1 * regressionCity.slope;
const y2 = regressionCity.yIntercept + x2 * regressionCity.slope;
const regressionLine = [
{ decade: x1, meanTemp: y1 },
{ decade: x2, meanTemp: y2 },
];
const caption = \`Mean temperature per decade in \${city}. Linear regression: y = \${regressionCity.slope}x + \${regressionCity.yIntercept}, R² = \${regressionCity.r2}\`;
return plot({
width,
x: { label: "Time", tickFormat: (d) => String(d) },
y: { label: "Temperature", tickFormat: (d) => \`\${d}°C\`, ticks: 5 },
inset: 20,
grid: true,
marks: [
line(regressionLine, {
x: "decade",
y: "meanTemp",
stroke: "black",
strokeDasharray: "4,4",
}),
dot(tempCity, { x: "decade", y: "meanTemp", fill: "orange", r: 5 }),
],
caption,
});
}
`;
let getTempChangeTs =
`// It's important to use the 'web' entrypoint since this is running in the browser.
import { formatNumber } from "@nshiab/journalism/web";
export default function getTempChange(
city: string,
tempRegressions: { city: string; slope: number }[]
): string {
const cityRegression = tempRegressions.find((r) => r.city === city);
if (cityRegression === undefined) {
throw new Error(\`City \${city} not found in tempRegressions.\`);
}
const slopPerDecade = cityRegression.slope * 10;
return formatNumber(slopPerDecade, { decimals: 3, suffix: "°C" });
}
`;
console.log("\n2 - Creating relevant files...");
if (existsSync("README.md")) {
console.log(" => README.md already exists.");
} else {
writeFileSync("README.md", readme);
console.log(" => README.md has been created.");
}
if (runtime === "bun") {
packageJson.scripts.sda = "bun --watch sda/main.ts";
} else if (runtime === "deno") {
packageJson.scripts.sda =
"deno run --node-modules-dir=auto -A --watch sda/main.ts";
}
if (runtime === "deno") {
if (existsSync("deno.json")) {
console.log(" => deno.json already exists.");
const currentDenoJson = JSON.parse(
readFileSync("./deno.json", "utf-8"),
);
currentDenoJson.tasks = {
...currentDenoJson.tasks,
...denoJson.tasks,
};
currentDenoJson.nodeModulesDir = "auto";
writeFileSync("deno.json", JSON.stringify(currentDenoJson, null, 2));
console.log(" => deno.json has been updated.");
} else {
writeFileSync("deno.json", JSON.stringify(denoJson, null, 2));
console.log(" => deno.json has been created.");
}
} else {
if (existsSync("package.json")) {
console.log(" => package.json already exists.");
const currentPackageJson = JSON.parse(
readFileSync("./package.json", "utf-8"),
);
currentPackageJson.scripts = {
...currentPackageJson.scripts,
...packageJson.scripts,
};
writeFileSync(
"package.json",
JSON.stringify(currentPackageJson, null, 2),
);
console.log(" => package.json has been updated.");
} else {
writeFileSync("package.json", JSON.stringify(packageJson, null, 2));
console.log(" => package.json has been created.");
}
}
if (existsSync("observablehq.config.js")) {
console.log(" => observablehq.config.js already exists.");
} else {
writeFileSync("observablehq.config.js", observableConfigJS);
console.log(" => observablehq.config.js has been created.");
}
if (existsSync(".gitignore")) {
console.log(" => .gitignore already exists.");
} else {
writeFileSync(
".gitignore",
"dist/\nnode_modules/\nyarn-error.log\n.temp\n.sda-cache\ndata-raw\n.DS_Store",
);
console.log(" => .gitignore has been created.");
}
if (existsSync("sda")) {
console.log(" => sda/ already exists.");
} else {
mkdirSync("sda");
writeFileSync("sda/main.ts", mainTs);
console.log(" => sda/main.ts has been created.");
mkdirSync("sda/functions");
writeFileSync("sda/functions/crunchData.ts", crunchDataTs);
console.log(" => sda/functions/crunchData.ts has been created.");
mkdirSync("sda/data-raw");
writeFileSync("sda/data-raw/temp.csv", data);
console.log(" => sda/data-raw/temp.csv has been created.");
}
if (existsSync("src")) {
console.log(" => src/ already exists.");
} else {
mkdirSync("src");
writeFileSync("src/index.md", indexMd);
console.log(" => src/index.md has been created.");
writeFileSync("src/.gitignore", "/.observablehq/cache/");
console.log(" => src/.gitignore has been created.");
mkdirSync("src/data");
console.log(" => src/data/ has been created.");
writeFileSync("src/data/temp.json", tempJSON);
console.log(" => src/data/temp.json has been created.");
writeFileSync("src/data/temp-regressions.json", tempRegressionsJSON);
console.log(" => src/data/temp-regressions.json has been created.");
mkdirSync("src/components");
writeFileSync("src/components/createChart.ts", createChartTs);
console.log(" => src/components/createChart.ts has been created.");
mkdirSync("src/helpers");
if (runtime === "deno") {
getTempChangeTs = getTempChangeTs.replace(
`import { formatNumber } from "@nshiab/journalism/web";`,
`import { formatNumber } from "jsr:@nshiab/journalism/web";`,
);
}
writeFileSync("src/helpers/getTempChange.ts", getTempChangeTs);
console.log(" => src/helpers/getTempChange.ts has been created.");
}
if (runtime === "nodejs") {
console.log("\n3 - Installing libraries with NPM...");
execSync("npm i rimraf --save-dev", {
stdio: "ignore",
});
console.log(" => rimraf has been installed from NPM.");
execSync("npm i @observablehq/framework", {
stdio: "ignore",
});
console.log(" => @observablehq/framework has been installed from NPM.");
execSync("npm i @observablehq/plot", {
stdio: "ignore",
});
console.log(" => @observablehq/plot has been installed from NPM.");
execSync("npx jsr add @nshiab/simple-data-analysis", {
stdio: "ignore",
});
console.log(" => simple-data-analysis has been installed from JSR.");
execSync("npx jsr add @nshiab/journalism", {
stdio: "ignore",
});
console.log(" => journalism has been installed from JSR.");
} else if (runtime === "bun") {
console.log("\n3 - Installing libraries with Bun...");
execSync("bun add rimraf --dev", {
stdio: "ignore",
});
console.log(" => rimraf has been installed from NPM.");
execSync("bun add @observablehq/framework", {
stdio: "ignore",
});
console.log(" => @observablehq/framework has been installed from NPM.");
execSync("bun add @observablehq/plot", {
stdio: "ignore",
});
console.log(" => @observablehq/plot has been installed from NPM.");
execSync("bunx jsr add @nshiab/simple-data-analysis", {
stdio: "ignore",
});
console.log(" => simple-data-analysis has been installed from JSR.");
execSync("bunx jsr add @nshiab/journalism", {
stdio: "ignore",
});
console.log(" => journalism has been installed from JSR.");
} else if (runtime === "deno") {
console.log("\n3 - Installing libraries with Deno...");
execSync(
"deno install --node-modules-dir=auto --allow-scripts=npm:playwright-chromium,npm:duckdb jsr:@nshiab/simple-data-analysis",
{
stdio: "ignore",
},
);
console.log(" => simple-data-analysis has been installed from JSR.");
execSync(
"deno install --node-modules-dir=auto --allow-scripts=npm:playwright-chromium jsr:@nshiab/journalism",
{
stdio: "ignore",
},
);
console.log(" => journalism has been installed from JSR.");
execSync("deno install --node-modules-dir=auto --dev npm:rimraf", {
stdio: "ignore",
});
console.log(" => rimraf has been installed from NPM.");
execSync(
"deno install --node-modules-dir=auto npm:@observablehq/framework",
{
stdio: "ignore",
},
);
console.log(" => @observablehq/framework has been installed from NPM.");
execSync("deno install --node-modules-dir=auto npm:@observablehq/plot", {
stdio: "ignore",
});
console.log(" => @observablehq/plot has been installed from NPM.");
}
if (args.includes("--git")) {
console.log("\n4 - Initializing Git repository...");
execSync("git init && git add -A && git commit -m 'Setup done'", {
stdio: "ignore",
});
console.log(" => First commit done");
}
console.log("\nSetup is done!\n");
if (runtime === "nodejs") {
console.log(" => Run 'npm run sda' to watch main.ts.");
console.log(
" => Run 'npm run dev' to start a local server and watch index.md.",
);
} else if (runtime === "bun") {
console.log(" => Run 'bun run sda' to watch main.ts.");
console.log(
" => Run 'bun run dev' to start a local server and watch index.md.",
);
} else if (runtime === "deno") {
console.log(" => Run 'deno task sda' to watch main.ts.");
console.log(
" => Run 'deno task dev' to start a local server and watch index.md.",
);
}
console.log("\nHave fun. ^_^\n");
} else if (args.includes("--svelte")) {
console.log(` => You passed the option --svelte`);
const readme =
`This repository has been created with [setup-sda](https://github.com/nshiab/setup-sda/).
It's using [simple-data-analysis](https://github.com/nshiab/simple-data-analysis), [journalism](https://github.com/nshiab/journalism), and others great open-source librairies with [SvelteKit](https://svelte.dev/docs/kit/introduction).
Here's the recommended workflow:
- Put your raw data in the \`sda/data-raw\` folder. Note that this folder is gitignored.
- Use the \`sda/main.ts\` file to clean and process your raw data. Write the results to the \`src/data\` or \`static/\` folders.
- Import your processed data from the \`src/data\` folder into the \`src/routes/+page.svelte\` or fetch it with \`src/routes/+page.js\`.
- Use the data for your content.
When working on your project, you can use the following commands:
- \`npm run sda\` will watch your \`sda/main.ts\` and its dependencies. Everytime you'll save some changes, the data will be reprocessed.
- \`npm run dev\` will start a local server and watch all \`src/*\` files and their dependencies. Everytime you'll save some changes or the data is reprocessed, the content will be updated.
By opening two terminals each running one of the above commands, you'll be able to work on your project with a live preview of your content and data.
`;
const packageJson = {
type: "module",
scripts: {
"dev": "vite dev",
"build": "vite build",
"preview": "vite preview",
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
"check:watch":
"svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
sda: "node --experimental-strip-types --no-warnings --watch sda/main.ts",
clean: "rm -rf .sda-cache && rm -rf .temp",
},
};
const denoJson = {
tasks: {
"dev": "vite dev",
"build": "vite build",
"preview": "vite preview",
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
"check:watch":
"svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
sda: "deno run --node-modules-dir=auto -A --watch sda/main.ts",
clean: "rm -rf .sda-cache && rm -rf .temp",
},
nodeModulesDir: "auto",
compilerOptions: {
"lib": ["dom", "deno.ns"],
},
unstable: [
"fmt-component",
],
};
const tempCsv = `city,decade,meanTemp
Toronto,1840.0,7.1
Toronto,1850.0,6.4
Toronto,1860.0,6.9
Toronto,1870.0,6.8
Montreal,1880.0,5.3
Toronto,1880.0,6.6
Montreal,1890.0,6.0
Toronto,1890.0,7.6
Montreal,1900.0,5.8
Toronto,1900.0,7.7
Montreal,1910.0,6.1
Toronto,1910.0,8.1
Montreal,1920.0,6.2
Toronto,1920.0,8.0
Montreal,1930.0,6.8
Toronto,1930.0,8.6
Montreal,1940.0,6.9
Toronto,1940.0,8.7
Montreal,1950.0,7.4
Toronto,1950.0,9.2
Montreal,1960.0,7.4
Toronto,1960.0,8.8
Montreal,1970.0,7.3
Toronto,1970.0,9.0
Montreal,1980.0,7.7
Toronto,1980.0,9.0
Toronto,1990.0,9.6
Montreal,2000.0,7.6
Toronto,2000.0,9.7
Montreal,2010.0,8.0
Toronto,2010.0,9.9`;
const tempJSON = `[
{"city":"Toronto","decade":1840,"meanTemp":7.1},
{"city":"Toronto","decade":1850,"meanTemp":6.4},
{"city":"Toronto","decade":1860,"meanTemp":6.9},
{"city":"Toronto","decade":1870,"meanTemp":6.8},
{"city":"Montreal","decade":1880,"meanTemp":5.3},
{"city":"Toronto","decade":1880,"meanTemp":6.6},
{"city":"Montreal","decade":1890,"meanTemp":6.0},
{"city":"Toronto","decade":1890,"meanTemp":7.6},
{"city":"Montreal","decade":1900,"meanTemp":5.8},
{"city":"Toronto","decade":1900,"meanTemp":7.7},
{"city":"Montreal","decade":1910,"meanTemp":6.1},
{"city":"Toronto","decade":1910,"meanTemp":8.1},
{"city":"Montreal","decade":1920,"meanTemp":6.2},
{"city":"Toronto","decade":1920,"meanTemp":8.0},
{"city":"Montreal","decade":1930,"meanTemp":6.8},
{"city":"Toronto","decade":1930,"meanTemp":8.6},
{"city":"Montreal","decade":1940,"meanTemp":6.9},
{"city":"Toronto","decade":1940,"meanTemp":8.7},
{"city":"Montreal","decade":1950,"meanTemp":7.4},
{"city":"Toronto","decade":1950,"meanTemp":9.2},
{"city":"Montreal","decade":1960,"meanTemp":7.4},
{"city":"Toronto","decade":1960,"meanTemp":8.8},
{"city":"Montreal","decade":1970,"meanTemp":7.3},
{"city":"Toronto","decade":1970,"meanTemp":9.0},
{"city":"Montreal","decade":1980,"meanTemp":7.7},
{"city":"Toronto","decade":1980,"meanTemp":9.0},
{"city":"Toronto","decade":1990,"meanTemp":9.6},
{"city":"Montreal","decade":2000,"meanTemp":7.6},
{"city":"Toronto","decade":2000,"meanTemp":9.7},
{"city":"Montreal","decade":2010,"meanTemp":8.0},
{"city":"Toronto","decade":2010,"meanTemp":9.9}
]`;
const tempRegressionsJSON = `[
{"city":"Toronto","x":"decade","y":"meanTemp","slope":0.0203,"yIntercept":-30.7911,"r2":0.9261},
{"city":"Montreal","x":"decade","y":"meanTemp","slope":0.0196,"yIntercept":-31.3115,"r2":0.921}
]`;
const mainTs = `import { SimpleDB } from "@nshiab/simple-data-analysis";
import crunchData from "./functions/crunchData.ts";
const sdb = new SimpleDB({ logDuration: true });
await crunchData(sdb);
await sdb.done();`;
const crunchDataTs =
`import type { SimpleDB } from "@nshiab/simple-data-analysis";
export default async function crunchData(sdb: SimpleDB) {
// The mean temperature per decade.
const temp = sdb.newTable("temp");
await temp.loadData("sda/data-raw/temp.csv");
await temp.logTable();
// We compute a linear regression for each city.
const tempRegressions = await temp.linearRegressions({
x: "decade",
y: "meanTemp",
categories: "city",
decimals: 4,
outputTable: "tempRegressions",
});
await tempRegressions.logTable();
// We write the results to src/data
await tempRegressions.writeData("src/data/temp-regressions.json");
// Or to static
await temp.writeData("static/temp.json");
}`;
const viteConfigTs = `import { sveltekit } from "@sveltejs/kit/vite";
import { defineConfig } from "vite";
export default defineConfig({
plugins: [sveltekit()],
});
`;
const tsconfigJson = `{
"extends": "./.svelte-kit/tsconfig.json",
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"sourceMap": true,
"strict": true,
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"rootDirs": ["..", "./types"]
}
// Path aliases are handled by https://svelte.dev/docs/kit/configuration#alias
// except $lib which is handled by https://svelte.dev/docs/kit/configuration#files
//
// If you want to overwrite includes/excludes, make sure to copy over the relevant includes/excludes
// from the referenced tsconfig.json - TypeScript does not merge them in
}
`;
const svelteConfigJs = `import adapter from "@sveltejs/adapter-static";
import { vitePreprocess } from "@sveltejs/vite-plugin-svelte";
/** @type {import('@sveltejs/kit').Config} */
const config = {
// Consult https://svelte.dev/docs/kit/integrations
// for more information about preprocessors
preprocess: vitePreprocess(),
kit: {
adapter: adapter({
// default options are shown. On some platforms
// these options are set automatically — see below
pages: "build",
assets: "build",
fallback: undefined,
precompress: false,
strict: true,
}),
},
};
export default config;`;
const gitignore = `node_modules
# Output
.output
.vercel
/.svelte-kit
/build
# OS
.DS_Store
Thumbs.db
# Env
.env
.env.*
!.env.example
!.env.test
# Vite
vite.config.js.timestamp-*
vite.config.ts.timestamp-*
# SDA
.temp
.sda-cache
data-raw`;
const styleCss = `/* Adapted from https://github.com/kevquirk/simple.css */
/* Global variables. */
:root {
/* Set sans-serif & mono fonts */
--sans-font:
-apple-system, BlinkMacSystemFont, "Avenir Next", Avenir, "Nimbus Sans L",
Roboto, "Noto Sans", "Segoe UI", Arial, Helvetica, "Helvetica Neue",
sans-serif;
--mono-font: Consolas, Menlo, Monaco, "Andale Mono", "Ubuntu Mono", monospace;
--standard-border-radius: 5px;
/* Default (light) theme */
--bg: #fff;
--accent-bg: #f5f5f5;
/* --accent-bg: #f5f7ff; */
--text: #212121;
--text-light: #585858;
--border: #898ea4;
--accent: #0d47a1;
--accent-hover: #1266e2;
--accent-text: var(--bg);
--code: #d81b60;
--preformatted: #444;
--marked: #ffdd33;
--disabled: #efefef;
}
/* Dark theme */
/* @media (prefers-color-scheme: dark) {
:root {
color-scheme: dark;
--bg: #212121;
--accent-bg: #2b2b2b;
--text: #dcdcdc;
--text-light: #ababab;
--accent: #ffb300;
--accent-hover: #ffe099;
--accent-text: var(--bg);
--code: #f06292;
--preformatted: #ccc;
--disabled: #111;
}
img,
video {
opacity: 0.8;
}
} */
/* Reset box-sizing */
*, *::before, *::after {
box-sizing: border-box;
}
/* Reset default appearance */
textarea,
select,
input,
progress {
appearance: none;
-webkit-appearance: none;
-moz-appearance: none;
}
html {
/* Set the font globally */
font-family: var(--sans-font);
scroll-behavior: smooth;
}
/* Make the body a nice central block */
body {
color: var(--text);
background-color: var(--bg);
font-size: 1.15rem;
line-height: 1.5;
display: grid;
grid-template-columns: 1fr min(45rem, 90%) 1fr;
margin: 0;
}
body > div > * {
grid-column: 2;
}
/* Make the header bg full width, but the content inline with body */
body > div > header {
background-color: var(--accent-bg);
border-bottom: 1px solid var(--border);
text-align: center;
padding: 0 0.5rem 2rem 0.5rem;
grid-column: 1 / -1;
}
body > div > header > *:only-child {
margin-block-start: 2rem;
}
body > div > header h1 {
max-width: 1200px;
margin: 1rem auto;
}
body > div > header p {
max-width: 40rem;
margin: 1rem auto;
}
/* Add a little padding to ensure spacing is correct between content and header > nav */
main {
padding-top: 1.5rem;
}
body > div > footer {
margin-top: 4rem;
padding: 2rem 1rem 1.5rem 1rem;
color: var(--text-light);
font-size: 0.9rem;
text-align: center;
border-top: 1px solid var(--border);
}
/* Format headers */
h1 {
font-size: 3rem;
}
h2 {
font-size: 2.6rem;
margin-top: 3rem;
}
h3 {
font-size: 2rem;
margin-top: 3rem;
}
h4 {
font-size: 1.44rem;
}
h5 {
font-size: 1.15rem;
}
h6 {
font-size: 0.96rem;
}
p {
margin: 1.5rem 0;
}