-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpackage.js
More file actions
241 lines (200 loc) · 6.18 KB
/
Copy pathpackage.js
File metadata and controls
241 lines (200 loc) · 6.18 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
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
/* eslint-disable no-console */
const fs = require( 'fs' )
const path = require( 'path' )
const archiver = require( 'archiver' )
const THEME_SLUG = 'start-stackable'
const BUILD_ROOT = 'build'
const BUILD_DIR = path.join( BUILD_ROOT, THEME_SLUG )
const DIST_DIR = 'dist'
let folderSuffix = ''
if ( process.argv.length === 3 ) {
folderSuffix = process.argv[ process.argv.length - 1 ]
}
const INDEX_PHP_CONTENT = `<?php
// Silence is golden.
`
const INCLUDED_FILES = [
'style.css',
'theme.json',
'readme.txt',
'functions.php',
'screenshot.png',
'screenshot.jpg',
]
const INCLUDED_DIRS = [
'templates',
'parts',
'styles',
'patterns',
'assets',
'fonts',
'languages',
]
function ensureDir( dir ) {
if ( ! fs.existsSync( dir ) ) {
fs.mkdirSync( dir, { recursive: true } )
}
}
function copyFile( src, dest ) {
ensureDir( path.dirname( dest ) )
fs.copyFileSync( src, dest )
}
function copyDir( src, dest ) {
if ( ! fs.existsSync( src ) ) {
return
}
ensureDir( dest )
for ( const item of fs.readdirSync( src ) ) {
if ( item.startsWith( '.' ) || item.endsWith( '.map' ) ) {
continue
}
const srcPath = path.join( src, item )
const destPath = path.join( dest, item )
const stat = fs.statSync( srcPath )
if ( stat.isDirectory() ) {
copyDir( srcPath, destPath )
} else {
copyFile( srcPath, destPath )
}
}
}
function addSecurityIndexFiles( dir ) {
if ( ! fs.existsSync( dir ) ) {
return
}
for ( const item of fs.readdirSync( dir ) ) {
const itemPath = path.join( dir, item )
if ( ! fs.statSync( itemPath ).isDirectory() ) {
continue
}
const indexPath = path.join( itemPath, 'index.php' )
if ( ! fs.existsSync( indexPath ) ) {
fs.writeFileSync( indexPath, INDEX_PHP_CONTENT )
}
addSecurityIndexFiles( itemPath )
}
}
function readThemeVersion() {
const styleCss = fs.readFileSync( 'style.css', 'utf8' )
const versionMatch = styleCss.match( /^Version:\s*([^\r\n]+)/m )
if ( ! versionMatch ) {
throw new Error( 'Could not find Version in style.css' )
}
return versionMatch[ 1 ].trim()
}
function applyVersionSuffix( buildDir, suffix ) {
if ( ! suffix ) {
return
}
const stylePath = path.join( buildDir, 'style.css' )
let style = fs.readFileSync( stylePath, 'utf8' )
style = style.replace(
/^(Version:\s*)([^\r\n]+)/m,
( match, prefix, version ) => {
if ( ! version.includes( suffix ) ) {
return prefix + version + '-' + suffix
}
return match
}
)
fs.writeFileSync( stylePath, style )
console.log( `Updated packaged style.css Version with suffix: ${ suffix }` )
}
function syncPackagedReadmeStableTag( buildDir ) {
const stylePath = path.join( buildDir, 'style.css' )
const readmePath = path.join( buildDir, 'readme.txt' )
if ( ! fs.existsSync( stylePath ) || ! fs.existsSync( readmePath ) ) {
return
}
const style = fs.readFileSync( stylePath, 'utf8' )
const versionMatch = style.match( /^Version:\s*([^\r\n]+)/m )
if ( ! versionMatch ) {
return
}
const version = versionMatch[ 1 ].trim()
const readme = fs.readFileSync( readmePath, 'utf8' )
if ( ! /^Stable tag:\s*[^\r\n]+/m.test( readme ) ) {
console.log( 'Could not find "Stable tag:" line in packaged readme.txt' )
return
}
const updated = readme.replace(
/^Stable tag:\s*[^\r\n]+/m,
`Stable tag: ${ version }`
)
if ( updated === readme ) {
console.log( `Packaged readme.txt Stable tag already ${ version }` )
return
}
fs.writeFileSync( readmePath, updated )
console.log( `Updated packaged readme.txt Stable tag to ${ version }` )
}
async function packageTheme() {
const themeVersion = readThemeVersion()
const packagedVersion = folderSuffix ? `${ themeVersion }-${ folderSuffix }` : themeVersion
const zipFolderName = THEME_SLUG + ( folderSuffix ? `-${ folderSuffix }` : '' )
console.log( 'Starting theme packaging...' )
console.log( `Version: ${ packagedVersion }` )
if ( fs.existsSync( BUILD_ROOT ) ) {
fs.rmSync( BUILD_ROOT, { recursive: true } )
}
ensureDir( BUILD_DIR )
console.log( 'Copying theme files...' )
for ( const file of INCLUDED_FILES ) {
if ( fs.existsSync( file ) && fs.statSync( file ).isFile() ) {
copyFile( file, path.join( BUILD_DIR, file ) )
}
}
for ( const dir of INCLUDED_DIRS ) {
if ( fs.existsSync( dir ) && fs.statSync( dir ).isDirectory() ) {
copyDir( dir, path.join( BUILD_DIR, dir ) )
}
}
if ( ! fs.existsSync( path.join( BUILD_DIR, 'functions.php' ) ) ) {
throw new Error( 'Packaged theme is missing functions.php' )
}
if ( fs.existsSync( path.join( BUILD_DIR, 'function.php' ) ) ) {
throw new Error( 'Packaged theme must not include function.php' )
}
if ( ! fs.existsSync( path.join( BUILD_DIR, 'style.css' ) ) ) {
throw new Error( 'Packaged theme is missing style.css' )
}
if ( ! fs.existsSync( path.join( BUILD_DIR, 'theme.json' ) ) ) {
throw new Error( 'Packaged theme is missing theme.json' )
}
if ( ! fs.existsSync( path.join( BUILD_DIR, 'templates', 'index.html' ) ) ) {
throw new Error( 'Packaged theme is missing templates/index.html' )
}
if ( fs.existsSync( 'src' ) && ! fs.existsSync( path.join( BUILD_DIR, 'assets', 'build', 'frontend.asset.php' ) ) ) {
throw new Error( 'Compiled assets missing. Run npm run compile before packaging.' )
}
applyVersionSuffix( BUILD_DIR, folderSuffix )
syncPackagedReadmeStableTag( BUILD_DIR )
console.log( 'Adding directory index.php files...' )
addSecurityIndexFiles( BUILD_DIR )
console.log( 'Creating zip package...' )
ensureDir( DIST_DIR )
const zipPath = path.join( DIST_DIR, `${ THEME_SLUG }-${ packagedVersion }.zip` )
if ( fs.existsSync( zipPath ) ) {
fs.unlinkSync( zipPath )
}
const output = fs.createWriteStream( zipPath )
const archive = archiver( 'zip', { zlib: { level: 9 } } )
const done = new Promise( ( resolve, reject ) => {
output.on( 'close', resolve )
output.on( 'error', reject )
archive.on( 'error', reject )
} )
archive.pipe( output )
archive.directory( BUILD_DIR, zipFolderName )
await archive.finalize()
await done
const size = ( archive.pointer() / 1024 / 1024 ).toFixed( 2 )
console.log( 'Theme packaged successfully.' )
console.log( `Theme tree: ${ BUILD_DIR }` )
console.log( `Package: ${ zipPath }` )
console.log( `Size: ${ size } MB` )
}
packageTheme().catch( err => {
console.error( err )
process.exit( 1 )
} )