Skip to content

Commit

Permalink
refactored prepublish script & implemented postpublish script
Browse files Browse the repository at this point in the history
  • Loading branch information
uguraslan committed Jan 31, 2024
1 parent e63a77e commit b4456f2
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 22 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"test:run": "tape '{,!(node_modules)/**/}*.test.js' | tap-diff",
"lint": "eslint '**/*.js'",
"lint:fix": "eslint '**/*.js' --fix",
"prepublishOnly": "node scripts/prepublishOnly.js"
"prepublishOnly": "node scripts/prepublishOnly.js",
"postpublish": "node scripts/postpublish.js"
},
"lint-staged": {
"*.js": [
Expand Down
59 changes: 59 additions & 0 deletions scripts/postpublish.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2023 Comcast Cable Communications Management, LLC
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

import fs from 'fs'
import path from 'path'

const currentDir = process.cwd()
const componentDirs = ['src/components']

function precompileCleanUp() {
for (const dir of componentDirs) {
const fullDir = path.resolve(currentDir, dir)
console.log(`Checking files in ${fullDir} for original files that precompiler created`)
processDirectory(fullDir)
}
console.log('Finished restoring original files after precompile')
}

function processDirectory(directory) {
const files = fs.readdirSync(directory)

for (const file of files) {
const filePath = path.join(directory, file)
const stat = fs.statSync(filePath)

if (stat.isDirectory()) {
processDirectory(filePath)
} else if (/\.(orig\.(js|ts))$/.test(file)) {
restoreOriginalFile(filePath)
}
}
}

function restoreOriginalFile(filePath) {
const originalFilePath = filePath.replace(/\.orig\.(j|t)s$/, '.$1s')
console.log(`Restoring original file ${originalFilePath}`)

// Restore the original file by copying the backup file over it
fs.copyFileSync(filePath, originalFilePath)

// Delete the backup file
fs.unlinkSync(filePath)
}

precompileCleanUp()
51 changes: 30 additions & 21 deletions scripts/prepublishOnly.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,47 @@ import compiler from '../src/lib/precompiler/precompiler.js'
import { exec } from 'child_process'

const currentDir = process.cwd()
const targetDir = path.resolve(currentDir, 'src')
const componentDirs = ['src/components']

function precompileComponents(directory) {
console.log(`Checking files in ${directory} for components to precompile`)
function precompileComponents() {
for (const dir of componentDirs) {
const fullDir = path.resolve(currentDir, dir)
console.log(`Checking files in ${fullDir} for components to precompile`)
processDirectory(fullDir)
}
console.log('Finished processing files suitable for precompilation')
}

function processDirectory(directory) {
const files = fs.readdirSync(directory)

for (const file of files) {
const filePath = path.join(directory, file)
const filePathRelative = path.relative(currentDir, filePath)
const stat = fs.statSync(filePath)

if (stat.isDirectory()) {
if (componentDirs.includes(filePathRelative)) {
precompileComponents(filePath)
}
} else if (
componentDirs.includes(path.dirname(filePathRelative)) &&
(file.endsWith('.js') || file.endsWith('.ts'))
) {
const source = fs.readFileSync(filePath, 'utf-8')
const newSource = compiler(source, filePath)
fs.writeFileSync(filePath, newSource)

// only format the file if it was changed
if (source !== newSource) {
formatFileWithESLint(filePath)
}
processDirectory(filePath)
} else if (/^(?!.*\.orig\.(js|ts)$).*\.(js|ts)$/.test(file)) {
// only process files that don't end in .orig.js or .orig.ts
processFile(filePath)
}
}
}

console.log('Finished processing files suitable for precompilation')
function processFile(filePath) {
console.log(`Precompiling ${filePath}`)
// backup the file
const backupFilePath = filePath.replace(/\.(j|t)s$/, '.orig.$1s')
fs.copyFileSync(filePath, backupFilePath)

const source = fs.readFileSync(filePath, 'utf-8')
const newSource = compiler(source, filePath)
fs.writeFileSync(filePath, newSource)

// only format the file if it was changed
if (source !== newSource) {
formatFileWithESLint(filePath)
}
}

function formatFileWithESLint(filePath) {
Expand All @@ -72,4 +81,4 @@ function formatFileWithESLint(filePath) {
})
}

precompileComponents(targetDir)
precompileComponents()

0 comments on commit b4456f2

Please sign in to comment.