-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.js
49 lines (38 loc) · 1.27 KB
/
init.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
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const SERVICE_WORKER_BUILD_PATH = path.resolve(
__dirname,
"src/oauth-service-worker.js"
);
const REDIRECT_HTML_BUILD_PATH = path.resolve(
__dirname,
"src/oauth-redirect.html"
);
const CWD = process.cwd();
const args = process.argv.slice(2);
const publicDir = args[1] || "./public";
// When running as a part of "postinstall" script, "cwd" equals the library's directory.
// The "postinstall" script resolves the right absolute public directory path.
const absolutePublicDir = path.isAbsolute(publicDir)
? publicDir
: path.resolve(CWD, publicDir);
const dirExists = fs.existsSync(absolutePublicDir);
if (!dirExists) {
// print error and exit
console.log("Public directory does not exist!");
process.exit(1);
}
console.log(
'Initializing the OAuth2 Service Worker at "%s"...',
absolutePublicDir
);
function copyFile(buildPath) {
const fileName = path.basename(buildPath);
const destinationFilePath = path.resolve(absolutePublicDir, fileName);
fs.copyFileSync(buildPath, destinationFilePath);
}
copyFile(SERVICE_WORKER_BUILD_PATH);
console.log("oauth-service-worker successfully created!");
copyFile(REDIRECT_HTML_BUILD_PATH);
console.log("oauth-redirect.html successfully created!");