Skip to content

Commit

Permalink
add: release mjs
Browse files Browse the repository at this point in the history
  • Loading branch information
Mansi1 committed May 23, 2024
1 parent 8f95227 commit b948bf3
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
"lint:fix": "prettier --write .",
"prebuild": "yarn clean",
"build": "yarn node --enable-source-maps ./build.mjs",
"prerelease:package": "rm -rf ./release",
"release:package": "mkdir release && cp -r dist package.json LICENSE README.md release/",
"release:package": "yarn node --enable-source-maps ./release.mjs",
"pre-commit": "yarn lint",
"pre-push": "yarn lint"
},
Expand Down
77 changes: 77 additions & 0 deletions release.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { access, rmSync, promises as fs } from 'fs'
import path from 'path'

const exist = async (path) => {
return new Promise((res) => {
access(path, (err) => {
if (!!err) {
res(false)
} else {
res(true)
}
})
})
}

const copyFiles = async (sourceFolder, destinationFolder) => {
try {
// Check if the source folder exists
await fs.access(sourceFolder)

// Create the destination folder if it doesn't exist
await fs.mkdir(destinationFolder, { recursive: true })

// Read all files from the source folder
const files = await fs.readdir(sourceFolder)

// Loop through each file and copy it to the destination folder
for (const file of files) {
const sourceFile = path.join(sourceFolder, file)
const destinationFile = path.join(destinationFolder, file)

// Check if the current item is a file or a folder
const stat = await fs.stat(sourceFile)
if (stat.isFile()) {
await fs.copyFile(sourceFile, destinationFile)
console.log(`Copied file: ${file}`)
} else if (stat.isDirectory()) {
// Recursively copy subdirectory
await copyFiles(sourceFile, destinationFile)
}
}

console.log(`All files copied from "${sourceFolder}" to "${destinationFolder}"`)
} catch (error) {
console.error(`An error occurred: ${error.message}`)
}
}
const CURRENT_DIR = process.cwd()
const DIST_FOLDER = path.join(CURRENT_DIR, 'dist')
const RELEASE_FOLDER = path.join(CURRENT_DIR, 'release')

//mkdir release && cp -r dist package.json LICENSE README.md release/
const main = async () => {
if (!(await exist(DIST_FOLDER))) {
throw Error(`Missing build folder '${DIST_FOLDER}'`)
}

//remove folder
if (await exist(RELEASE_FOLDER)) {
rmSync(RELEASE_FOLDER, { recursive: true, force: true })
}

//create folder
await copyFiles(DIST_FOLDER, path.join(RELEASE_FOLDER, 'dist'))
await fs.copyFile(path.join(CURRENT_DIR, 'LICENSE'), path.join(RELEASE_FOLDER, 'LICENSE'))
await fs.copyFile(path.join(CURRENT_DIR, 'README.md'), path.join(RELEASE_FOLDER, 'README.md'))

//readin package.json
const packageJsonRaw = await fs.readFile(path.join(CURRENT_DIR, 'package.json'))
const packageJson = JSON.parse(packageJsonRaw.toString())
delete packageJson['scripts']
delete packageJson['devDependencies']
delete packageJson['dependencies']

await fs.writeFile(path.join(RELEASE_FOLDER, 'package.json'), JSON.stringify(packageJson, null, 2))
}
main()

0 comments on commit b948bf3

Please sign in to comment.