refactor: components cdn (#1110)

* refactor(wip): separate components files into zips and assets dir

* refactor(wip): use new components package and cdn in mobile

* chore: add components to metro config

* chore: bump snjs with new web assets path

* refactor: exclude package.json files recursively from being copied into components dist folder to avoid conflicts with react native duplicates
This commit is contained in:
Mo
2022-06-15 16:00:23 -05:00
committed by GitHub
parent d7b61e0376
commit 566f6e1432
633 changed files with 295 additions and 1065 deletions

View File

@@ -5,10 +5,11 @@ import { spawnSync as spawn } from 'child_process'
import { GetFeatures } from '@standardnotes/features/dist/Domain/Feature/Features.js'
import { GetDeprecatedFeatures } from '@standardnotes/features/dist/Domain/Feature/Lists/DeprecatedFeatures.js'
import zip from '@standardnotes/deterministic-zip'
import minimatch from 'minimatch'
import {fileURLToPath} from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
import { fileURLToPath } from 'url'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
console.log('Beginning packaging procedure...')
@@ -19,9 +20,12 @@ if (specificFeatureIdentifier) {
const SourceFilesPath = path.join(__dirname, '../src')
const DistDir = path.join(__dirname, '../dist')
const TmpDir = path.join(__dirname, '../tmp')
const ZipsDir = path.join(DistDir, '/zips')
const AssetsDir = path.join(DistDir, '/assets')
const ChecksumsSrcPath = path.join(ZipsDir, 'checksums.json')
const ChecksumsDistPath = path.join(ZipsDir, 'checksums.json')
const ChecksumsSrcPath = path.join(DistDir, 'Checksums.json')
const ChecksumsDistPath = path.join(DistDir, 'Checksums.json')
const Checksums = JSON.parse(fs.readFileSync(ChecksumsSrcPath).toString())
console.log('Loaded existing checksums from', ChecksumsSrcPath)
@@ -34,7 +38,7 @@ async function zipDirectory(sourceDir, outPath) {
})
}
const copyFileOrDir = (src, dest) => {
const copyFileOrDir = (src, dest, exludedFilesGlob) => {
const isDir = fs.lstatSync(src).isDirectory()
if (isDir) {
ensureDirExists(dest)
@@ -42,11 +46,22 @@ const copyFileOrDir = (src, dest) => {
for (const entry of entries) {
const srcPath = path.join(src, entry.name)
const excluded = exludedFilesGlob && minimatch(srcPath, exludedFilesGlob)
if (excluded) {
console.log('Excluding file', srcPath)
continue
}
const destPath = path.join(dest, entry.name)
entry.isDirectory() ? copyFileOrDir(srcPath, destPath) : fs.copyFileSync(srcPath, destPath)
}
} else {
const excluded = exludedFilesGlob && minimatch(src, exludedFilesGlob)
if (excluded) {
console.log('Excluding file', src)
return
}
fs.copyFileSync(src, dest)
}
}
@@ -67,30 +82,27 @@ const emptyExistingDir = (dir) => {
}
}
const copyToDist = async (feature) => {
const copyComponentAssets = async (feature, destination, exludedFilesGlob) => {
const srcComponentPath = path.join(SourceFilesPath, feature.identifier)
if (!doesDirExist(srcComponentPath)) {
return
return false
}
const targetComponentPath = `${path.join(DistDir, feature.identifier)}`
emptyExistingDir(targetComponentPath)
ensureDirExists(targetComponentPath)
emptyExistingDir(destination)
ensureDirExists(destination)
for (const file of feature.static_files) {
const srcFilePath = path.join(srcComponentPath, file)
if (!fs.existsSync(srcFilePath)) {
continue
}
const targetFilePath = path.join(targetComponentPath, file)
copyFileOrDir(srcFilePath, targetFilePath)
const targetFilePath = path.join(destination, file)
copyFileOrDir(srcFilePath, targetFilePath, exludedFilesGlob)
}
return targetComponentPath
return true
}
const computeChecksum = async (zipPath, version) => {
@@ -109,17 +121,24 @@ const computeChecksum = async (zipPath, version) => {
const zipAndChecksumFeature = async (feature) => {
console.log('Processing feature', feature.identifier, '...')
const distPath = await copyToDist(feature)
if (!distPath) {
const assetsLocation = `${path.join(AssetsDir, feature.identifier)}`
const assetsSuccess = await copyComponentAssets(feature, assetsLocation, '**/package.json')
if (!assetsSuccess) {
return
}
const outZip = `${distPath}/${feature.identifier}.zip`
await zipDirectory(distPath, outZip)
const zipAssetsTmpLocation = `${path.join(TmpDir, feature.identifier)}`
const zipAssetsSuccess = await copyComponentAssets(feature, zipAssetsTmpLocation)
if (!zipAssetsSuccess) {
return
}
const checksum = await computeChecksum(outZip, feature.version)
const zipDestination = `${ZipsDir}/${feature.identifier}.zip`
await zipDirectory(zipAssetsTmpLocation, zipDestination)
const checksum = await computeChecksum(zipDestination, feature.version)
Checksums[feature.identifier] = checksum
console.log(`Computed checksums for ${feature.identifier}:`, checksum)
}
@@ -148,6 +167,9 @@ await (async () => {
}
fs.writeFileSync(ChecksumsSrcPath, JSON.stringify(Checksums, undefined, 2))
console.log('Succesfully wrote checksums to', ChecksumsSrcPath)
copyFileOrDir(ChecksumsSrcPath, ChecksumsDistPath)
console.log('Succesfully wrote checksums to', ChecksumsSrcPath)
emptyExistingDir(TmpDir)
})()