diff --git a/.gitignore b/.gitignore index 3579d87..fcfe5cf 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,5 @@ results results.json bulkDelete.json + +test.js \ No newline at end of file diff --git a/yarn2npm/yarn2npm.js b/yarn2npm/yarn2npm.js index e1af293..17c7ee8 100644 --- a/yarn2npm/yarn2npm.js +++ b/yarn2npm/yarn2npm.js @@ -5,51 +5,57 @@ const http = require('isomorphic-git/http/node'); const gitHubAuthFunct = () => { return { username: process.env.GITHUB_AUTH_TOKEN, password: '' }; }; const org = 'jupiterone'; -const repo = process.argv.slice(2); +const repoName = process.argv.slice(2); const prbranch = "yarn2npm-patch-1"; -const tmpDir="yarn2npm_tmp"; +const repoPath= './yarn2npm_tmp'; const fs = require('fs'); const path = require("path"); -const dir = `${tmpDir}/${repo}`; -const backupDir = `${tmpDir}/${repo}/yarn2npm`; +const dir = `${repoPath}/${repoName}`; +const backupDir = 'yarn2npm'; const main = async () => { try { + const origPath = process.cwd(); //Create tmp working - if (!fs.existsSync(tmpDir)){ - fs.mkdirSync(tmpDir); + if (!fs.existsSync(repoPath)){ + fs.mkdirSync(repoPath); } //Create github branch - await createYarn2NpmBranch(repo, dir, org) + await createYarn2NpmBranch(repoName, dir, org) if (fs.existsSync(`${dir}/package-lock.json`)){ - console.log(`${repo} is already configured for npm`); + console.log(`${repoName} is already configured for npm`); return[]; } //Create backup dir - if (!fs.existsSync(backupDir)){ - fs.mkdirSync(backupDir); + if (!fs.existsSync(`${dir}/${backupDir}`)){ + fs.mkdirSync(`${dir}/${backupDir}`); } //Backup package.json - fs.copyFile(`${dir}/package.json`, `${backupDir}/package.json`, (err) => { + let copyFile = 'orig-package.json'; + console.log(`Backing up package.json to ${copyFile}`); + fs.copyFile(`${dir}/package.json`, `${dir}/${backupDir}/${copyFile}`, (err) => { if (err) throw err; }); - + await git.add({ fs, dir, filepath: `${backupDir}/${copyFile}` }); + //remove yarn.lock + console.log(`Removing yarn.lock`); fs.unlink(`${dir}/yarn.lock`, (err) => { if (err) throw err; }); + await git.remove({ fs, dir, filepath: 'yarn.lock' }); - var currentPath = process.cwd(); - process.chdir(dir); + //npm install console.log(`Running npm install`); + process.chdir(dir); try{ execSync('npm install', {stdio: 'inherit'}, function(error) { if (error) { @@ -59,8 +65,12 @@ const main = async () => { } catch(e){ console.log(`Error running npm install. Creating lock file ${backupDir}/failed.lock`); - fs.closeSync(fs.openSync(`${currentPath}/${backupDir}/failed.lock`, 'a')); + fs.closeSync(fs.openSync(`${origPath}/${backupDir}/failed.lock`, 'a')); + await git.add({ fs, dir: `${origPath}/${backupDir}`, filepath: 'failed.lock' }); } + process.chdir(origPath); + await git.add({ fs, dir, filepath: 'package-lock.json' }); + //Grab the npm log const npmlogDir = (`${require('os').homedir()}/.npm/_logs/`); @@ -70,24 +80,25 @@ const main = async () => { .sort((a, b) => b.mtime.getTime() - a.mtime.getTime()))[0].file; console.log(`Backing up npm logs`); - fs.copyFile (`${npmlogDir}/${newestLog}`, `${currentPath}/${backupDir}/${newestLog}`, (err) => { + fs.copyFile (`${npmlogDir}/${newestLog}`, `${dir}/${backupDir}/${newestLog}`, (err) => { if (err) throw err; }); + await git.add({ fs, dir, filepath: `${backupDir}/${newestLog }` }); //Find and replace yarn commands console.log(`Replacing yarn commands with npm`); - const packageFile = fs.readFileSync(`package.json`, { + const packageFile = fs.readFileSync(`${dir}/package.json`, { encoding: 'utf8', flag: 'r', }) .toString().replace(/yarn/g,'npm run') - fs.writeFile(`package.json`, packageFile, 'utf8', function (err) { + fs.writeFile(`${dir}/package.json`, packageFile, 'utf8', function (err) { if (err) return console.log(err); }); - - // Check to see if changes needs to be pushed - await pushChanges(`${currentPath}/${dir}`); + await git.add({ fs, dir, filepath: 'package.json' }); + + await pushChanges(dir); return []; } catch (e) {