Skip to content

Commit

Permalink
feat: revamp ui (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Jan 9, 2022
1 parent 89c90f8 commit bfe42f5
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 87 deletions.
5 changes: 4 additions & 1 deletion package.json
Expand Up @@ -52,6 +52,9 @@
"typescript": "^4.5.4"
},
"eslintConfig": {
"extends": "@pvtnbr"
"extends": "@pvtnbr",
"rules": {
"@typescript-eslint/no-shadow": ["error", { "allow": ["task"] }]
}
}
}
186 changes: 100 additions & 86 deletions src/index.ts
Expand Up @@ -76,7 +76,7 @@ const { stringify } = JSON;
dry: {
type: Boolean,
alias: 'd',
description: 'Dry run mode',
description: 'Dry run mode. Will not build, commit, or push to the remote.',
},
},

Expand All @@ -94,124 +94,138 @@ const { stringify } = JSON;
}

const packageJson = await readJson(packageJsonPath);

const {
builtBranch = `built/${branchFrom}`,
buildCommand,
remote,
dry,
} = argv.flags;

if (dry) {
console.log('Running in dry mode');
}

const localBuiltBranch = `build-this-branch/${builtBranch}-${Date.now()}`;
let success = false;
try {
let distributionFiles: string[] = [];

// In the try-finally block in case it modifies the working tree
// On failure, they will be reverted by the hard reset
await task('Building branch', async ({ setTitle }) => {
await task(
`Building branch ${stringify(branchFrom)}${stringify(builtBranch)}`,
async ({
task, setTitle, setStatus, setOutput,
}) => {
if (dry) {
return;
setStatus('Dry run');
}

await promisify(childProcess.exec)(argv.flags.buildCommand);
const localBuiltBranch = `build-this-branch/${builtBranch}-${Date.now()}`;
let success = false;
try {
let distributionFiles: string[] = [];

// In the try-finally block in case it modifies the working tree
// On failure, they will be reverted by the hard reset
const createBuild = await task(`Creating build with ${stringify(buildCommand)}`, async ({ setWarning }) => {
if (dry) {
setWarning('');
return;
}

await promisify(childProcess.exec)(buildCommand);

distributionFiles = await packlist();

if (distributionFiles.length === 0) {
throw new Error('No distribution files found');
}

/**
* Remove "prepack" script
* https://github.com/npm/cli/issues/1229#issuecomment-699528830
*
* Upon installing a git dependency, the prepack script is run
* without devdependency installation.
*/
if (packageJson.scripts && 'prepack' in packageJson.scripts) {
delete packageJson.scripts.prepack;
await fs.promises.writeFile(packageJsonPath, stringify(packageJson, null, 2));
}
});

if (!dry) {
createBuild.clear();
}

distributionFiles = await packlist();
const checkoutBranch = await task(`Checking out branch ${stringify(builtBranch)}`, async ({ setWarning }) => {
if (dry) {
setWarning('');
return;
}

if (distributionFiles.length === 0) {
throw new Error('No distribution files found');
}
await execa('git', ['checkout', '--orphan', localBuiltBranch]);

/**
* Remove "prepack" script
* https://github.com/npm/cli/issues/1229#issuecomment-699528830
*
* Upon installing a git dependency, the prepack script is run
* without devdependency installation.
*/
if (packageJson.scripts && 'prepack' in packageJson.scripts) {
delete packageJson.scripts.prepack;
await fs.promises.writeFile(packageJsonPath, stringify(packageJson, null, 2));
}
// Unstage all files
await execa('git', ['reset']);
});

setTitle('Built branch');
});
if (!dry) {
checkoutBranch.clear();
}

await task(`Checking out branch ${stringify(builtBranch)}`, async ({ setTitle }) => {
if (dry) {
return;
}
const commit = await task('Commiting distribution assets', async ({ setWarning }) => {
if (dry) {
setWarning('');
return;
}

await execa('git', ['checkout', '--orphan', localBuiltBranch]);
await execa('git', ['add', '-f', ...distributionFiles]);
await execa('git', ['commit', '-nm', `Built from ${stringify(branchFrom)}`]);
});

// Unstage all files
await execa('git', ['reset']);
if (!dry) {
commit.clear();
}

setTitle(`Checked out branch ${stringify(builtBranch)}`);
});
const push = await task(
`Force pushing branch ${stringify(builtBranch)} to remote ${stringify(remote)}`,
async ({ setWarning }) => {
if (dry) {
setWarning('');
return;
}

const numberOfDistributionFiles = distributionFiles.length.toLocaleString();
await execa('git', ['push', '-f', remote, `${localBuiltBranch}:${builtBranch}`]);

await task(`Commiting ${numberOfDistributionFiles} distribution files`, async ({ setTitle }) => {
if (dry) {
return;
}
success = true;
},
);

await execa('git', ['add', '-f', ...distributionFiles]);
await execa('git', ['commit', '-nm', `Built from ${stringify(branchFrom)}`]);

setTitle(`Commiting ${numberOfDistributionFiles} distribution files`);
});

await task(
`Force pushing branch ${stringify(builtBranch)} to remote ${stringify(remote)}`,
async ({ setTitle }) => {
if (dry) {
return;
if (!dry) {
push.clear();
}
} finally {
const revertBranch = await task(`Switching branch back to ${stringify(branchFrom)}`, async ({ setWarning }) => {
if (dry) {
setWarning('');
return;
}

await execa('git', ['push', '-f', remote, `${localBuiltBranch}:${builtBranch}`]);

setTitle(`Force pushed branch ${stringify(builtBranch)} to remote ${stringify(remote)}`);
success = true;
},
);
} finally {
await task(`Reverting branch to ${stringify(branchFrom)}`, async ({ setTitle }) => {
if (dry) {
return;
}

// In case commit failed and there are uncommitted changes
await execa('git', ['reset', '--hard']);
// In case commit failed and there are uncommitted changes
await execa('git', ['reset', '--hard']);

await execa('git', ['checkout', '-f', branchFrom]);
await execa('git', ['checkout', '-f', branchFrom]);

// Delete local built branch
await execa('git', ['branch', '-D', localBuiltBranch]);
// Delete local built branch
await execa('git', ['branch', '-D', localBuiltBranch]);
});

setTitle(`Reverted branch to ${stringify(branchFrom)}`);
});
}
revertBranch.clear();
}

if (success) {
await task(
'Generating npm install command',
async ({ setTitle, setOutput }) => {
if (success) {
const { stdout } = await execa('git', ['remote', 'get-url', remote]);
const parsedGitUrl = stdout.match(/github\.com:(.+)\.git$/);

if (parsedGitUrl) {
const [, repo] = parsedGitUrl;
setTitle('Install with command:');
setTitle('Successfully built branch! Install with command:');
setOutput(`npm i '${repo}#${builtBranch}'`);
}
},
);
}
}
},
);
})().catch((error) => {
console.log('Error:', error.message);

Expand Down

0 comments on commit bfe42f5

Please sign in to comment.