Skip to content

Commit 4a1c20e

Browse files
committed
#2710 simplify the build scripts
1 parent 0a0dee4 commit 4a1c20e

File tree

10 files changed

+265
-18
lines changed

10 files changed

+265
-18
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,6 @@ Thumbs.db
5454
pdf.*.mjs
5555
viewer-*.mjs
5656
sbom.json
57+
.env
58+
/ftp/.env
59+
.htaccess

angular.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@
7979
"input": "dist/ngx-extended-pdf-viewer/",
8080
"output": "/assets/extended-pdf-viewer/changelog/"
8181
},
82-
"projects/showcase/src/assets/search-index.json"
82+
"projects/showcase/src/assets/search-index.json",
83+
"projects/showcase/src/robots.txt"
8384
],
8485
"styles": ["projects/showcase/src/styles.css", "node_modules/prismjs/themes/prism-okaidia.css"],
8586
"scripts": [
@@ -111,7 +112,7 @@
111112
{
112113
"type": "anyComponentStyle",
113114
"maximumWarning": "2kb",
114-
"maximumError": "4kb"
115+
"maximumError": "44kb"
115116
}
116117
],
117118
"outputHashing": "all"

build-tools/6-release-showcase.sh

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22
# navigate to the root directory, no matter where the script is called
33
source "$(dirname "$0")/99-cd-to-root.sh"
44

5-
echo "to be continued..."
5+
npm run build:showcase
6+
cp ftp/.htaccess dist/showcase/browser
7+
cd ftp
8+
npx tsx upload.ts

ftp/upload.ts

+168
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
import * as ftp from 'basic-ftp';
2+
import * as fs from 'fs';
3+
import * as path from 'path';
4+
require('dotenv').config();
5+
6+
console.log(process.env.ftphost);
7+
8+
const remoteRootFolder = '/pdfviewer.net/relaunch';
9+
// const remoteRootFolder = '/angularfaces';
10+
11+
synchronizeDistFolderWithFtpFolder();
12+
13+
function collectFiles(dir: string, allFiles: string[] = [], allFolders: string[] = []) {
14+
const files = fs.readdirSync(dir);
15+
16+
files.forEach((file) => {
17+
const filePath = path.join(dir, file);
18+
if (fs.statSync(filePath).isDirectory()) {
19+
allFolders.push(filePath);
20+
collectFiles(filePath, allFiles, allFolders);
21+
} else {
22+
allFiles.push(filePath);
23+
}
24+
});
25+
26+
return allFiles;
27+
}
28+
29+
async function collectRemoteFiles(client: ftp.Client, dir: string, allFiles: any, allFolders: any) {
30+
console.log('Reading ' + dir);
31+
await client.ensureDir(dir);
32+
let files;
33+
try {
34+
files = await client.list(dir);
35+
} catch {
36+
console.log('Retry...');
37+
files = await client.list(dir);
38+
}
39+
for (const file of files) {
40+
if (file !== 'relaunch') {
41+
const filePath = dir + '/' + file.name;
42+
if (file.isDirectory) {
43+
allFolders[filePath] = file;
44+
await collectRemoteFiles(client, filePath, allFiles, allFolders);
45+
} else {
46+
allFiles[filePath] = file;
47+
}
48+
}
49+
}
50+
51+
return allFiles;
52+
}
53+
54+
async function synchronizeDistFolderWithFtpFolder() {
55+
const allFiles: string[] = [];
56+
const allFolders: string[] = [];
57+
collectFiles('../dist/showcase/browser', allFiles, allFolders);
58+
console.log(allFolders.length + ' folders');
59+
console.log(allFiles.length + ' files');
60+
const client = new ftp.Client();
61+
62+
try {
63+
await client.access({
64+
host: process.env.ftphost,
65+
user: process.env.ftpuser,
66+
password: process.env.ftppassword,
67+
secure: true,
68+
});
69+
70+
const allRemoteFiles = {};
71+
const allRemoteFolders = {};
72+
console.log('Collecting the remote files (this takes 1-2 minutes)');
73+
await collectRemoteFiles(client, remoteRootFolder, allRemoteFiles, allRemoteFolders);
74+
console.log('--------');
75+
console.log(Object.keys(allRemoteFolders).length + ' remote folders');
76+
console.log(allFolders.length + ' local folders');
77+
console.log(Object.keys(allRemoteFiles).length + ' remote files');
78+
console.log(allFiles.length + ' local files');
79+
80+
console.log('--------');
81+
82+
await client.cd(remoteRootFolder);
83+
let i = 0;
84+
85+
for (const folder of allFolders) {
86+
const remoteFolder = folder.replace('../dist/showcase/browser', remoteRootFolder);
87+
if (!allRemoteFolders[remoteFolder]) {
88+
console.log(i++ + ' Creating folder ' + remoteFolder);
89+
90+
try {
91+
await client.ensureDir(remoteFolder);
92+
} catch (exception) {
93+
console.log('Retry...');
94+
await client.ensureDir(remoteFolder);
95+
}
96+
}
97+
}
98+
99+
await client.cd(remoteRootFolder);
100+
i = 0;
101+
for (const file of allFiles) {
102+
const remoteFile = file.replace('../dist/showcase/browser', remoteRootFolder);
103+
try {
104+
if (allRemoteFiles[remoteFile]) {
105+
const info = fs.statSync(file);
106+
const remoteInfo = allRemoteFiles[remoteFile] as ftp.FileInfo;
107+
if (info.size === remoteInfo.size && !file.includes('index.html') && !file.includes('.mjs')) {
108+
// console.log("Skipping " + remoteFile);
109+
} else {
110+
// console.log("Different size");
111+
console.log(i++ + ' of ' + allFiles.length + ' Uploading (updated)' + remoteFile);
112+
try {
113+
await client.uploadFrom(file, remoteFile);
114+
} catch (exception) {
115+
console.log('Retry...');
116+
await client.uploadFrom(file, remoteFile);
117+
}
118+
}
119+
} else {
120+
console.log(i++ + ' of ' + allFiles.length + ' Uploading (new) ' + remoteFile);
121+
try {
122+
await client.uploadFrom(file, remoteFile);
123+
} catch (exception) {
124+
console.log('Retry...');
125+
await client.uploadFrom(file, remoteFile);
126+
}
127+
}
128+
} catch (exception) {
129+
console.log("Couldn't upload " + file);
130+
console.log(exception);
131+
}
132+
}
133+
134+
for (const remoteFile of Object.keys(allRemoteFiles)) {
135+
if (remoteFile) {
136+
if (!remoteFile.includes('.htaccess')) {
137+
const localFile = remoteFile.replace(remoteRootFolder, '../dist/showcase/browser');
138+
if (!allFiles.includes(localFile)) {
139+
console.log('delete ' + remoteFile);
140+
try {
141+
await client.remove(remoteFile);
142+
} catch (exception) {
143+
console.log('Retry...');
144+
await client.remove(remoteFile);
145+
}
146+
}
147+
}
148+
}
149+
}
150+
for (const remoteFolder of Object.keys(allRemoteFolders)) {
151+
if (remoteFolder) {
152+
const localFolder = remoteFolder.replace(remoteRootFolder, '../dist/showcase/browser');
153+
if (!allFolders.includes(localFolder)) {
154+
console.log('delete ' + remoteFolder);
155+
try {
156+
await client.removeDir(remoteFolder);
157+
} catch (exception) {
158+
console.log('Retry...');
159+
await client.removeDir(remoteFolder);
160+
}
161+
}
162+
}
163+
}
164+
} catch (err) {
165+
console.log(err);
166+
}
167+
client.close();
168+
}

package-lock.json

+23
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"run:both:old": "./build-tools/24-build-library-and-old-showcase.sh --open",
1919
"run:all:old": "./build-tools/124-build-base-library-and-library-and-old-showcase.sh --open",
2020
"release:lib": "./build-tools/5-release-library.sh",
21-
"release:showcase": "./build-tools/6-release-library.sh",
21+
"release:showcase": "./build-tools/6-release-showcase.sh",
2222
"--- showcase tasks:": "",
2323
"build-schematics": "npm run build --prefix projects/showcase-schematics",
2424
"add-documentation-page": "schematics ./projects/showcase-schematics:documentationPage --dry-run=false",
@@ -65,6 +65,8 @@
6565
"@types/node": "^22.10.5",
6666
"@types/trusted-types": "^2.0.7",
6767
"autoprefixer": "^10.4.20",
68+
"basic-ftp": "^5.0.5",
69+
"dotenv": "^16.4.7",
6870
"jest": "^29.7.0",
6971
"jest-environment-jsdom": "^29.7.0",
7072
"jest-preset-angular": "^14.4.2",
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1-
import { Component, input } from '@angular/core';
1+
import { Component, inject, input } from '@angular/core';
22
import { MarkdownComponent } from 'ngx-markdown';
3+
import { BaseHrefService } from '../services/base-href.service';
34

45
@Component({
56
selector: 'pvs-markdown',
67
standalone: true,
78
imports: [MarkdownComponent],
8-
template: ` <markdown [src]="src()" [data]="data()"></markdown> `,
9+
template: ` <markdown [src]="prefix + src()" [data]="data()"></markdown> `,
910
})
1011
export class MarkdownContentComponent {
1112
src = input<string>();
1213
data = input<string>();
14+
15+
public prefix = '';
16+
17+
private baseHrefService = inject(BaseHrefService);
18+
19+
public constructor() {
20+
this.prefix = this.baseHrefService.getBaseHref();
21+
}
1322
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { isPlatformBrowser } from '@angular/common';
2+
import { Inject, Injectable, PLATFORM_ID } from '@angular/core';
3+
4+
@Injectable({
5+
providedIn: 'root',
6+
})
7+
export class BaseHrefService {
8+
constructor(@Inject(PLATFORM_ID) private platformId: Object) {}
9+
10+
public getBaseHref(): string {
11+
if (isPlatformBrowser(this.platformId)) {
12+
const baseElement: HTMLBaseElement | null = document.querySelector('base');
13+
if (baseElement) {
14+
const href = baseElement.getAttribute('href') ?? '';
15+
if (href === '/') {
16+
return '';
17+
}
18+
return href;
19+
}
20+
return '';
21+
}
22+
// Return a default value when rendering on the server
23+
return '';
24+
}
25+
}

projects/showcase/src/index.html

+23-12
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
<!doctype html>
22
<html lang="en">
3-
<head>
4-
<meta charset="utf-8" />
5-
<title>Showcase</title>
6-
<base href="/" />
7-
<meta name="viewport" content="width=device-width, initial-scale=1" />
8-
<link rel="icon" type="image/x-icon" href="favicon.ico" />
9-
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,[email protected],100..700,0..1,-50..200" />
10-
</head>
11-
<body class="max-lg:has-[.sidebar-open]:blocked-scroll bg-background dark:bg-background-dark dark:text-on-background-dark">
12-
<pvs-root></pvs-root>
13-
</body>
14-
</html>
3+
4+
<head>
5+
<meta charset="utf-8" />
6+
<title>Showcase</title>
7+
<script>
8+
if (window.location.hostname === "localhost") {
9+
document.write("<base href=\"/\" />");
10+
} else {
11+
document.write("<base href=\"/relaunch/\" />");
12+
}
13+
</script>
14+
<meta name="viewport" content="width=device-width, initial-scale=1" />
15+
<link rel="icon" type="image/x-icon" href="favicon.ico" />
16+
<link rel="stylesheet"
17+
href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,[email protected],100..700,0..1,-50..200" />
18+
</head>
19+
20+
<body
21+
class="max-lg:has-[.sidebar-open]:blocked-scroll bg-background dark:bg-background-dark dark:text-on-background-dark">
22+
<pvs-root></pvs-root>
23+
</body>
24+
25+
</html>

projects/showcase/src/robots.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
User-agent: *
2+
Disallow: /relaunch/

0 commit comments

Comments
 (0)