-
Notifications
You must be signed in to change notification settings - Fork 3
/
publish.js
81 lines (79 loc) · 2.7 KB
/
publish.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
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
const config = require('./config.js')
const path = require('path')
const fs = require('fs')
const exec = require('child_process').exec
const tmpDir = require('os').tmpdir()
async function publish () {
function createFolder (folder) {
return new Promise((resolve, reject) => {
fs.mkdir(folder, async err => {
if (err) {
switch (err.code) {
case 'EEXIST':
break
case 'ENOENT':
try {
await createFolder(path.dirname(folder))
} catch (err) {
return reject(err)
}
break
default:
return reject(err)
}
}
console.log(`Folder ${folder} creado`)
return resolve(folder)
})
})
}
function copyFolder (srcFolder, destFolder) {
return new Promise((resolve, reject) => {
console.log(`Entrando en folder ${srcFolder}.`)
fs.readdir(srcFolder, async (err, files) => {
if (err) return reject(err)
if (files.length > 0) {
try {
await createFolder(destFolder)
await Promise.all(files.map(file => {
return new Promise((resolve, reject) => {
fs.copyFile(path.join(srcFolder, file), path.join(destFolder, file), err => {
if (err) return reject(err)
console.log(`Copiado archivo ${file}`)
return resolve(file)
})
})
}))
} catch (err) { return reject(err) }
}
return resolve(destFolder)
})
})
}
function renameFolder (currentName, newName) {
return new Promise((resolve, reject) => {
fs.rename(currentName, newName, function (err) {
if (err) return reject(err)
return resolve()
})
})
}
function moveFolderToTmp (folder) {
return new Promise((resolve, reject) => {
const hash = Date.now()
// Linux:
exec(`mv ${folder} ${path.join(tmpDir, `${path.basename(folder)}_${hash}`)}`, function (err) {
if (err) return reject(err)
return resolve()
})
})
}
console.log('\x1b[33m%s\x1b[0m', 'Copia de archivos estáticos')
await Promise.all(config.folders.map(async folder => {
await copyFolder(path.join(__dirname, 'src', folder), path.join(__dirname, 'public', folder)).catch(err => { throw err })
})).catch(err => { throw err })
await moveFolderToTmp(config.buildPaths.oldPath).catch(err => { throw err })
await renameFolder(config.buildPaths.publicPath, config.buildPaths.oldPath).catch(err => { throw err })
await renameFolder(path.join(__dirname, 'public'), config.buildPaths.publicPath).catch(err => { throw err })
}
publish()