Skip to content

Commit

Permalink
full test to git push.
Browse files Browse the repository at this point in the history
added test.js to ignore file
  • Loading branch information
electricgull committed Mar 9, 2023
1 parent f389d7c commit 0547906
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 21 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ results

results.json
bulkDelete.json

test.js
53 changes: 32 additions & 21 deletions yarn2npm/yarn2npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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/`);
Expand All @@ -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) {
Expand Down

0 comments on commit 0547906

Please sign in to comment.