chore(workflow): fix desktop checksums script

This commit is contained in:
Mo
2022-06-17 07:41:32 -05:00
parent aa45bd8a8c
commit e4bef17dfa
4 changed files with 51 additions and 24 deletions

View File

@@ -154,7 +154,10 @@ jobs:
with:
name: assets
path: packages/desktop/dist
- run: node scripts/sums.mjs
- name: Generate Checksums
run: node scripts/sums.mjs
- name: get-npm-version
id: package-version
uses: martinbeentjes/npm-get-version-action@main

View File

@@ -8,6 +8,8 @@ import zip from '@standardnotes/deterministic-zip'
import minimatch from 'minimatch'
import { fileURLToPath } from 'url'
import { ensureDirExists, doesDirExist, emptyExistingDir } from '../../../scripts/ScriptUtils.mjs'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
@@ -66,22 +68,6 @@ const copyFileOrDir = (src, dest, exludedFilesGlob) => {
}
}
const doesDirExist = (dir) => {
return fs.existsSync(dir)
}
const ensureDirExists = (dir) => {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true })
}
}
const emptyExistingDir = (dir) => {
if (fs.existsSync(dir)) {
fs.rmSync(dir, { recursive: true })
}
}
const getComponentSrcPath = (feature) => {
return path.join(SourceFilesPath, feature.identifier)
}

View File

@@ -1,23 +1,38 @@
import crypto from 'crypto'
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
const __filename = fileURLToPath(import.meta.url)
const ScriptsDir = path.dirname(__filename)
import { doesFileExist } from '../../../scripts/ScriptUtils.mjs'
function sha256(filePath) {
return new Promise((resolve, reject) => {
if (!doesFileExist(filePath)) {
console.log('Attempting to hash non-existing file', filePath)
return null
}
return new Promise((resolve) => {
try {
fs.createReadStream(filePath)
.pipe(crypto.createHash('sha256').setEncoding('hex'))
.on('finish', function () {
resolve(this.read())
})
.on('error', resolve(null))
.on('error', function () {
resolve(null)
})
} catch (error) {
console.log('Error reading file', error)
resolve(null)
}
})
}
async function getFileNames() {
const packageJson = await fs.promises.readFile('./package.json')
const packageJson = await fs.promises.readFile(path.join(ScriptsDir, '../package.json'))
const version = JSON.parse(packageJson).version
return [
`standard-notes-${version}-mac-x64.zip`,
@@ -64,18 +79,20 @@ process.on('uncaughtException', function (err) {
let hashes = await Promise.all(
files.map(async (fileName) => {
const filePath = path.join(ScriptsDir, `../dist/${fileName}`)
try {
const hash = await sha256(`dist/${fileName}`)
return `${hash} ${fileName}`
const hash = await sha256(filePath)
const entry = `${hash} ${fileName}`
return entry
} catch (error) {
console.error('Unable to hash file', fileName)
console.error('Unable to hash file', filePath)
return null
}
}),
)
hashes = hashes.join('\n')
await fs.promises.writeFile('dist/SHA256SUMS', hashes)
await fs.promises.writeFile(path.join(ScriptsDir, '../dist/SHA256SUMS'), hashes)
console.log(`Successfully wrote SHA256SUMS:\n${hashes}`)
} catch (err) {
console.error(err)

21
scripts/ScriptUtils.mjs Normal file
View File

@@ -0,0 +1,21 @@
import fs from 'fs'
export const doesDirExist = (dir) => {
return fs.existsSync(dir)
}
export const doesFileExist = (file) => {
return fs.existsSync(file)
}
export const ensureDirExists = (dir) => {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true })
}
}
export const emptyExistingDir = (dir) => {
if (fs.existsSync(dir)) {
fs.rmSync(dir, { recursive: true })
}
}