Skip to content

Commit 4ee22d6

Browse files
committed
fix(update): wait for each update to complete before applying the next one
1 parent e59a970 commit 4ee22d6

File tree

2 files changed

+54
-41
lines changed

2 files changed

+54
-41
lines changed

project/update.ts

Lines changed: 52 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import simpleGit, {SimpleGit} from "simple-git";
22
import chain from "@softwareventures/chain";
33
import {concat} from "@softwareventures/array";
4+
import {mapFn as mapAsyncFn} from "../collections/async-iterable";
45
import {FsStage, InsertResult} from "../fs-stage/fs-stage";
56
import {
67
bindAsyncResultFn,
@@ -15,7 +16,6 @@ import {
1516
import {emptyDirectory} from "../fs-stage/directory";
1617
import {updateCopyrightYear} from "../license/update-copyright-year";
1718
import {commit, CommitFailureReason} from "../fs-stage/commit";
18-
import {excludeNull, mapFn} from "../collections/async-iterable";
1919
import {addMissingLicense} from "../license/add-missing-license";
2020
import {YarnFixFailureReason} from "../yarn/fix";
2121
import {applyCodeStyle} from "../yarn/apply-code-style";
@@ -46,14 +46,8 @@ export type UpdateStepFailureReason = YarnFixFailureReason | PrettierFixFailureR
4646
export async function updateProject(project: Project): Promise<UpdateResult> {
4747
const git = simpleGit(project.path);
4848

49-
return chain([
50-
updateFixScript(project),
51-
applyCodeStyle(project),
52-
updateCopyrightYear(project),
53-
addMissingLicense(project)
54-
])
55-
.map(excludeNull)
56-
.map(mapFn(step(project, git)))
49+
return chain([updateFixScript, applyCodeStyle, updateCopyrightYear, addMissingLicense])
50+
.map(mapAsyncFn(step(project, git)))
5751
.map(combineAsyncResults).value;
5852
}
5953

@@ -66,40 +60,59 @@ export function gitNotClean(path: string): GitNotClean {
6660
return {type: "git-not-clean", path};
6761
}
6862

69-
function step(project: Project, git: SimpleGit): (update: Update) => Promise<UpdateResult> {
63+
function step(
64+
project: Project,
65+
git: SimpleGit
66+
): (update: (project: Project) => Promise<Update | null>) => Promise<UpdateResult> {
7067
return async update =>
7168
git
7269
.status()
7370
.then(status => (status.isClean() ? success() : failure([gitNotClean(project.path)])))
74-
.then(
75-
bindAsyncResultFn<GitNotClean, UpdateFailureReason>(async () =>
76-
update.type === "fs-stage-update"
77-
? update
78-
.apply({root: emptyDirectory, overwrite: true})
79-
.then(throwFailureFn("Internal error creating update file stage"))
80-
.then(async stage => commit(project.path, stage))
81-
: update.apply()
82-
)
83-
)
84-
.then(mapAsyncResultFn(async () => git.status()))
85-
.then(mapResultFn(status => concat([status.modified, status.not_added])))
86-
.then(
87-
bindAsyncResultFn(async files =>
88-
prettierFixFilesIfAvailable(project, files).then(mapResultFn(() => files))
89-
)
71+
.then(mapAsyncResultFn(async () => update(project)))
72+
.then(bindAsyncResultFn(async update => commitUpdate(project, git, update)));
73+
}
74+
75+
async function commitUpdate(
76+
project: Project,
77+
git: SimpleGit,
78+
update: Update | null
79+
): Promise<UpdateResult> {
80+
if (update == null) {
81+
return success();
82+
}
83+
84+
return writeUpdate(project, update)
85+
.then(mapAsyncResultFn(async () => git.status()))
86+
.then(mapResultFn(status => concat([status.modified, status.not_added])))
87+
.then(
88+
bindAsyncResultFn(async files =>
89+
prettierFixFilesIfAvailable(project, files).then(mapResultFn(() => files))
9090
)
91-
.then(
92-
mapAsyncResultFn(async files =>
93-
files.length === 0
94-
? undefined
95-
: git.add(files).then(async () => git.commit(update.log))
96-
)
91+
)
92+
.then(
93+
mapAsyncResultFn(async files =>
94+
files.length === 0
95+
? undefined
96+
: git.add(files).then(async () => git.commit(update.log))
9797
)
98-
.then(
99-
mapResultFn(commitResult => {
100-
if (commitResult != null) {
101-
console.log(`Applied update: ${update.log}`);
102-
}
103-
})
104-
);
98+
)
99+
.then(
100+
mapResultFn(commitResult => {
101+
if (commitResult != null) {
102+
console.log(`Applied update: ${update.log}`);
103+
}
104+
})
105+
);
106+
}
107+
108+
async function writeUpdate(project: Project, update: Update): Promise<UpdateResult> {
109+
switch (update.type) {
110+
case "fs-stage-update":
111+
return update
112+
.apply({root: emptyDirectory, overwrite: true})
113+
.then(throwFailureFn("Internal error creating update file stage"))
114+
.then(async stage => commit(project.path, stage));
115+
case "direct-update":
116+
return update.apply();
117+
}
105118
}

result/result.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export function mapResultFn<TReason, TValue, TNewValue>(
5757

5858
export async function mapAsyncResult<TReason, TValue, TNewValue>(
5959
result: Result<TReason, TValue>,
60-
f: (value: TValue) => PromiseLike<TNewValue>
60+
f: (value: TValue) => PromiseLike<TNewValue> | TNewValue
6161
): Promise<Result<TReason, TNewValue>> {
6262
if (result.type === "success") {
6363
return Promise.resolve(f(result.value)).then(value => ({type: "success", value}));
@@ -67,7 +67,7 @@ export async function mapAsyncResult<TReason, TValue, TNewValue>(
6767
}
6868

6969
export function mapAsyncResultFn<TReason, TValue, TNewValue>(
70-
f: (value: TValue) => PromiseLike<TNewValue>
70+
f: (value: TValue) => PromiseLike<TNewValue> | TNewValue
7171
): (result: Result<TReason, TValue>) => Promise<Result<TReason, TNewValue>> {
7272
return async result => mapAsyncResult(result, f);
7373
}

0 commit comments

Comments
 (0)