@@ -12,7 +12,7 @@ if (!shell.which('git')) {
1212}
1313
1414const cacheDir = 'pdf'
15- const repoRoot = 'pdf'
15+ let repoRoot = 'pdf'
1616const publicPDFRoot = path . join ( __dirname , '../assets/pdf' )
1717const pdfFiles = [
1818 'build/pdf.js' ,
@@ -27,22 +27,38 @@ const files = [...pdfFiles, ...pdfDirs]
2727
2828shell . cd ( path . resolve ( __dirname ) )
2929
30- shell . rm ( '-rf' , cacheDir )
30+ const distRoot = getPdfDistRoot ( )
31+ const useLocalDist =
32+ ! ! distRoot && fs . existsSync ( path . join ( distRoot , 'web/viewer.js' ) )
3133
32- exec (
33- `wget https://github.com/mozilla/pdf.js/releases/download/v2.16.105/pdfjs-2.16.105-dist.zip -O pdfjs.tar.gz &&
34- mkdir -p ${ cacheDir } &&
35- tar -xzvf pdfjs.tar.gz -C ${ cacheDir } ` ,
36- 'Error: download failed'
37- )
34+ if ( useLocalDist ) {
35+ shell . echo ( 'Using pdfjs-dist from node_modules.' )
36+ repoRoot = distRoot
37+ } else {
38+ shell . rm ( '-rf' , cacheDir )
39+
40+ shell . mkdir ( '-p' , cacheDir )
41+
42+ const zipName = 'pdfjs.zip'
43+ const zipPath = path . join ( __dirname , zipName )
44+
45+ const downloadCmd =
46+ `curl -L --retry 3 --retry-delay 2 --max-time 300 -k https://github.com/mozilla/pdf.js/releases/download/v2.16.105/pdfjs-2.16.105-dist.zip -o "${ zipPath } "` +
47+ ` || curl -L --retry 3 --retry-delay 2 --max-time 300 -k https://ghproxy.com/https://github.com/mozilla/pdf.js/releases/download/v2.16.105/pdfjs-2.16.105-dist.zip -o "${ zipPath } "` +
48+ ` || curl -L --retry 3 --retry-delay 2 --max-time 300 -k https://download.fastgit.org/mozilla/pdf.js/releases/download/v2.16.105/pdfjs-2.16.105-dist.zip -o "${ zipPath } "`
3849
39- shell . cd ( './' + cacheDir )
50+ exec ( downloadCmd , 'Error: download failed' )
51+
52+ exec ( `tar -xf "${ zipPath } " -C "${ cacheDir } "` , 'Error: download failed' )
53+
54+ repoRoot = path . join ( __dirname , cacheDir )
55+ }
4056
4157startUpgrade ( )
4258
4359async function startUpgrade ( ) {
4460 shell . echo ( '\nChecking files.' )
45- await Promise . all ( files . map ( p => exists ( path . join ( __dirname , repoRoot , p ) ) ) )
61+ await Promise . all ( files . map ( p => exists ( resolveRepoPath ( p ) ) ) )
4662
4763 shell . echo ( '\nModifying files.' )
4864 await Promise . all ( [ modifyViewrJS ( ) , modifyViewerHTML ( ) ] )
@@ -62,7 +78,7 @@ async function startUpgrade() {
6278}
6379
6480async function modifyViewrJS ( ) {
65- const viewerPath = path . join ( __dirname , repoRoot , 'web/viewer.js' )
81+ const viewerPath = resolveRepoPath ( 'web/viewer.js' )
6682 let file = await fs . readFile ( viewerPath , 'utf8' )
6783
6884 file = '/* saladict */ window.__SALADICT_PDF_PAGE__ = true;\n' + file
@@ -97,7 +113,7 @@ async function modifyViewrJS() {
97113}
98114
99115async function modifyViewerHTML ( ) {
100- const viewerPath = path . join ( __dirname , repoRoot , 'web/viewer.html' )
116+ const viewerPath = resolveRepoPath ( 'web/viewer.html' )
101117 let file = await fs . readFile ( viewerPath , 'utf8' )
102118
103119 if ( ! file . includes ( `</body>` ) ) {
@@ -126,6 +142,21 @@ function cleanInit() {
126142 } )
127143}
128144
145+ function getPdfDistRoot ( ) {
146+ try {
147+ return path . dirname ( require . resolve ( 'pdfjs-dist/package.json' ) )
148+ } catch ( e ) {
149+ return null
150+ }
151+ }
152+
153+ function resolveRepoPath ( subPath ) {
154+ if ( path . isAbsolute ( repoRoot ) ) {
155+ return path . join ( repoRoot , subPath )
156+ }
157+ return path . join ( __dirname , repoRoot , subPath )
158+ }
159+
129160async function exists ( path ) {
130161 try {
131162 await fs . access ( path )
@@ -152,26 +183,26 @@ async function cloneFiles() {
152183 for ( const pdfFile of pdfFiles ) {
153184 const targetPath = path . join ( publicPDFRoot , pdfFile )
154185 await fs . ensureFile ( targetPath )
155- await fs . copy ( path . join ( __dirname , repoRoot , pdfFile ) , targetPath )
186+ await fs . copy ( resolveRepoPath ( pdfFile ) , targetPath )
156187 }
157188
158189 const restPdfDirs = pdfDirs . filter ( name => name !== 'web/locale' )
159190
160191 for ( const pdfDir of restPdfDirs ) {
161192 const targetPath = path . join ( publicPDFRoot , pdfDir )
162193 await fs . ensureDir ( targetPath )
163- await fs . copy ( path . join ( __dirname , repoRoot , pdfDir ) , targetPath )
194+ await fs . copy ( resolveRepoPath ( pdfDir ) , targetPath )
164195 }
165196
166197 // copy locale.properties
167198 await fs . ensureDir ( path . join ( publicPDFRoot , 'web/locale' ) )
168199 await fs . copy (
169- path . join ( __dirname , repoRoot , 'web/locale/locale.properties' ) ,
200+ resolveRepoPath ( 'web/locale/locale.properties' ) ,
170201 path . join ( publicPDFRoot , 'web/locale/locale.properties' )
171202 )
172203
173204 const locales = (
174- await fs . readdir ( path . join ( __dirname , repoRoot , 'web/locale' ) )
205+ await fs . readdir ( resolveRepoPath ( 'web/locale' ) )
175206 ) . filter (
176207 name =>
177208 name . startsWith ( 'en' ) ||
@@ -182,9 +213,6 @@ async function cloneFiles() {
182213 for ( const locale of locales ) {
183214 const targetPath = path . join ( publicPDFRoot , 'web/locale' , locale )
184215 await fs . ensureDir ( targetPath )
185- await fs . copy (
186- path . join ( __dirname , repoRoot , 'web/locale' , locale ) ,
187- targetPath
188- )
216+ await fs . copy ( resolveRepoPath ( path . join ( 'web/locale' , locale ) ) , targetPath )
189217 }
190218}
0 commit comments