forked from sillsdev/appbuilder-pwa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvertManifest.ts
More file actions
91 lines (83 loc) · 3.46 KB
/
convertManifest.ts
File metadata and controls
91 lines (83 loc) · 3.46 KB
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
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
import path from 'path';
import { createHashedFile, createHashedFileFromContents } from './fileUtils';
import { Task, TaskOutput } from './Task';
export interface ManifestTaskOutput extends TaskOutput {
taskName: 'ConvertManifest';
}
/**
* Copies manifest.webmanifest to static folder
*/
export function convertManifest(dataDir: string, verbose: number) {
const srcFile = path.join(dataDir, 'manifest.json');
let contents = '';
const existing = existsSync(srcFile);
if (existing) {
mkdirSync(path.join('static', 'icons'), { recursive: true });
const fileContents = readFileSync(srcFile).toString();
const lines = fileContents.split(/\r?\n/);
const eol = fileContents.includes('\r\n') ? '\r\n' : '\n';
contents = lines
.map((line) => {
if (line.includes('start_url')) {
const urlPath = process.env.BUILD_BASE_PATH ? process.env.BUILD_BASE_PATH : '.';
line = ` "start_url" : "${urlPath}/",`;
}
if (line.includes('scope')) {
const scopePath = process.env.BUILD_BASE_PATH
? process.env.BUILD_BASE_PATH.endsWith('/')
? process.env.BUILD_BASE_PATH
: process.env.BUILD_BASE_PATH + '/'
: '/';
line = ` "scope" : "${scopePath}",`;
}
if (line.includes('"src":') && line.includes('./icons')) {
const srcMatch = line.match(/"src"\s*:\s*"\.\/([^"]+)"/);
if (!srcMatch) {
console.error(`Could not parse icon path from line: ${line}`);
return line;
}
const bareFileName = srcMatch[1];
let finalName = bareFileName;
const iconPath = path.join(dataDir, bareFileName);
if (existsSync(iconPath)) {
finalName = createHashedFile(dataDir, bareFileName, verbose);
} else {
throw new Error(`Required icon file ${iconPath} does not exist!`);
}
line = line.replace(srcMatch[0], `"src": "./${finalName}"`);
}
return line;
})
.join(eol);
} else {
// If no manifest exists, we need to at least have a minimum manifest to build.
const manifest = {
name: 'Temp PWA',
start_url: './',
display: 'standalone',
background_color: '#000000',
theme_color: '#000000'
};
contents = JSON.stringify(manifest);
}
const hashedName = createHashedFileFromContents(contents, 'manifest.json', verbose);
mkdirSync(path.join('src', 'gen-assets'), { recursive: true });
writeFileSync(
path.join('src/gen-assets', 'manifestUrl.json'),
JSON.stringify({ url: hashedName })
);
}
export class ConvertManifest extends Task {
public triggerFiles: string[] = ['manifest.json'];
constructor(dataDir: string) {
super(dataDir);
}
public async run(verbose: number, outputs: Map<string, TaskOutput>): Promise<TaskOutput> {
convertManifest(this.dataDir, verbose);
return {
taskName: this.constructor.name,
files: []
};
}
}