Skip to content

Commit ecdb99b

Browse files
committed
feat: add package update dependencies command
1 parent d25a57f commit ecdb99b

1 file changed

Lines changed: 172 additions & 151 deletions

File tree

lib/package.js

Lines changed: 172 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -619,202 +619,223 @@ export default class Package {
619619

620620
// XXX
621621
async updateDependencies ( { reinstall, commit, repeatOnError = false } = {} ) {
622+
if ( !this.dependencies.hasDependencies ) return result( 200 );
623+
622624
const logger = new Logger( {
623625
"stdout": "pipe",
624626
"stderr": "pipe",
625627
} );
626628

627-
if ( !this.dependencies.hasDependencies ) return result( 200 );
628-
629-
if ( reinstall ) {
630-
await fs.promises.rm( path.join( this.root, "node_modules" ), {
631-
"force": true,
632-
"recursive": true,
633-
} );
634-
}
635-
636-
const res = await this.npm.updateDependencies();
637-
if ( !res.ok ) return res;
629+
var res,
630+
updates = [];
638631

639-
const updates = [];
632+
try {
633+
if ( reinstall ) {
634+
await fs.promises.rm( path.join( this.root, "node_modules" ), {
635+
"force": true,
636+
"recursive": true,
637+
} );
638+
}
640639

641-
// added
642-
for ( const change of res.data.add ) {
643-
updates.push( {
644-
"name": change.name,
645-
"oldVersion": null,
646-
"newVersion": change.version,
647-
} );
648-
}
640+
res = await this.npm.updateDependencies();
641+
if ( !res.ok ) throw res;
649642

650-
// removed
651-
for ( const change of res.data.remove ) {
652-
updates.push( {
653-
"name": change.name,
654-
"oldVersion": change.version,
655-
"newVersion": null,
656-
} );
657-
}
643+
// added
644+
for ( const change of res.data.add ) {
645+
updates.push( {
646+
"name": change.name,
647+
"oldVersion": null,
648+
"newVersion": change.version,
649+
} );
650+
}
658651

659-
// changed
660-
for ( const change of res.data.change ) {
661-
updates.push( {
662-
"name": change.from.name,
663-
"oldVersion": change.from.version,
664-
"newVersion": change.to.version,
665-
} );
666-
}
652+
// removed
653+
for ( const change of res.data.remove ) {
654+
updates.push( {
655+
"name": change.name,
656+
"oldVersion": change.version,
657+
"newVersion": null,
658+
} );
659+
}
667660

668-
if ( updates.length ) {
669-
res.data.updates = updates;
661+
// changed
662+
for ( const change of res.data.change ) {
663+
updates.push( {
664+
"name": change.from.name,
665+
"oldVersion": change.from.version,
666+
"newVersion": change.to.version,
667+
} );
668+
}
670669

671-
const table = new Table( {
672-
"ansi": process.stdout,
673-
"width": process.stdout,
674-
"columns": {
675-
"name": {
676-
"title": ansi.hl( "DEPENDENCY" ),
677-
"headerAlign": "center",
678-
"headerValign": "end",
679-
},
680-
"oldVersion": {
681-
"title": ansi.hl( "OLD VERSION" ),
682-
"headerAlign": "center",
683-
"headerValign": "end",
684-
"align": "end",
685-
"width": 30,
686-
"format": ( value, row ) => {
687-
if ( value ) {
688-
if ( row.newVersion ) {
689-
return ` ${ value } `;
670+
if ( updates.length ) {
671+
const table = new Table( {
672+
"ansi": process.stdout,
673+
"width": process.stdout,
674+
"columns": {
675+
"name": {
676+
"title": ansi.hl( "DEPENDENCY" ),
677+
"headerAlign": "center",
678+
"headerValign": "end",
679+
},
680+
"oldVersion": {
681+
"title": ansi.hl( "OLD VERSION" ),
682+
"headerAlign": "center",
683+
"headerValign": "end",
684+
"align": "end",
685+
"width": 30,
686+
"format": ( value, row ) => {
687+
if ( value ) {
688+
if ( row.newVersion ) {
689+
return ` ${ value } `;
690+
}
691+
else {
692+
return ansi.error( ` ${ value } ` );
693+
}
690694
}
691695
else {
692-
return ansi.error( ` ${ value } ` );
696+
return " - ";
693697
}
694-
}
695-
else {
696-
return " - ";
697-
}
698+
},
698699
},
699-
},
700-
"newVersion": {
701-
"title": ansi.hl( "NEW VERSION" ),
702-
"headerAlign": "center",
703-
"headerValign": "end",
704-
"align": "end",
705-
"width": 30,
706-
"format": ( value, row ) => {
707-
if ( value ) {
708-
if ( row.oldVersion ) {
709-
return ` ${ value } `;
700+
"newVersion": {
701+
"title": ansi.hl( "NEW VERSION" ),
702+
"headerAlign": "center",
703+
"headerValign": "end",
704+
"align": "end",
705+
"width": 30,
706+
"format": ( value, row ) => {
707+
if ( value ) {
708+
if ( row.oldVersion ) {
709+
return ` ${ value } `;
710+
}
711+
else {
712+
return ansi.ok( ` ${ value } ` );
713+
}
710714
}
711715
else {
712-
return ansi.ok( ` ${ value } ` );
716+
return " - ";
713717
}
714-
}
715-
else {
716-
return " - ";
717-
}
718+
},
718719
},
719720
},
720-
},
721-
} )
722-
.write( updates.sort( ( a, b ) => compare( a.name, b.name ) ) )
723-
.end();
721+
} )
722+
.write( updates.sort( ( a, b ) => compare( a.name, b.name ) ) )
723+
.end();
724724

725-
logger.log( table.content );
725+
logger.log( table.content.trim() );
726726

727-
// commit and push
728-
COMMIT: if ( commit ) {
729-
try {
730-
let res;
727+
logger.log( "Update dependencies ... " + res );
728+
729+
// commit and push
730+
COMMIT: if ( commit ) {
731+
try {
731732

732-
// get working tree status
733-
res = await this.git.getWorkingTreeStatus();
734-
if ( !res.ok ) throw res;
733+
// get working tree status
734+
res = await this.git.getWorkingTreeStatus();
735+
if ( !res.ok ) throw res;
735736

736-
const commitFiles = [];
737+
const commitFiles = [];
737738

738-
// working tree is dirty
739-
if ( res.data.isDirty ) {
740-
for ( const lockFile of [ "package-lock.json", "npm-shrinkwrap.json" ] ) {
741-
const lockFilePath = this.rootSlug
742-
? this.rootSlug + "/" + lockFile
743-
: lockFile;
739+
// working tree is dirty
740+
if ( res.data.isDirty ) {
741+
for ( const lockFile of [ "package-lock.json", "npm-shrinkwrap.json" ] ) {
742+
const lockFilePath = this.rootSlug
743+
? this.rootSlug + "/" + lockFile
744+
: lockFile;
744745

745-
if ( res.data.files[ lockFilePath ] ) commitFiles.push( lockFile );
746+
if ( res.data.files[ lockFilePath ] ) commitFiles.push( lockFile );
747+
}
746748
}
747-
}
748749

749-
// dependencies locks was not updated
750-
if ( !commitFiles.length ) break COMMIT;
750+
// dependencies locks was not updated
751+
if ( !commitFiles.length ) break COMMIT;
751752

752-
// add changes
753-
res = await repeatAction(
754-
async () => {
755-
const res = await this.git.exec( [ "add", ...commitFiles ] );
753+
// add changes
754+
res = await repeatAction(
755+
async () => {
756+
const res = await this.git.exec( [ "add", ...commitFiles ] );
756757

757-
if ( res.ok ) {
758-
return res;
759-
}
760-
else {
758+
if ( res.ok ) {
759+
return res;
760+
}
761+
else {
761762

762-
// console.log( ansi.error( res + "" ) );
763+
// console.log( ansi.error( res + "" ) );
763764

764-
throw res;
765-
}
766-
},
767-
{ repeatOnError }
768-
);
769-
if ( !res.ok ) throw res;
765+
throw res;
766+
}
767+
},
768+
{ repeatOnError }
769+
);
770+
if ( !res.ok ) throw res;
770771

771-
// commit changes
772-
res = await repeatAction(
773-
async () => {
774-
const res = await this.git.exec( [ "commit", "-m", "chore(deps): update locked dependencies", ...commitFiles ] );
772+
// commit changes
773+
res = await repeatAction(
774+
async () => {
775+
const res = await this.git.exec( [ "commit", "-m", "chore(deps): update locked dependencies", ...commitFiles ] );
775776

776-
if ( res.ok ) {
777-
return res;
778-
}
779-
else {
777+
if ( res.ok ) {
778+
return res;
779+
}
780+
else {
780781

781-
// console.log( ansi.error( res + "" ) );
782+
// console.log( ansi.error( res + "" ) );
782783

783-
throw res;
784-
}
785-
},
786-
{ repeatOnError }
787-
);
788-
if ( !res.ok ) throw res;
784+
throw res;
785+
}
786+
},
787+
{ repeatOnError }
788+
);
789+
if ( !res.ok ) throw res;
789790

790-
// push changes
791-
res = await repeatAction(
792-
async () => {
793-
const res = await this.git.exec( [ "push" ] );
791+
// push changes
792+
res = await repeatAction(
793+
async () => {
794+
const res = await this.git.exec( [ "push" ] );
794795

795-
if ( res.ok ) {
796-
return res;
797-
}
798-
else {
796+
if ( res.ok ) {
797+
return res;
798+
}
799+
else {
799800

800-
// console.log( ansi.error( res + "" ) );
801+
// console.log( ansi.error( res + "" ) );
801802

802-
return res;
803-
}
804-
},
805-
{ repeatOnError }
806-
);
807-
if ( !res.ok ) throw res;
803+
return res;
804+
}
805+
},
806+
{ repeatOnError }
807+
);
808+
if ( !res.ok ) throw res;
808809

809-
logger.log( "Commit and push ... ", ansi.ok( " OK " ) );
810-
}
811-
catch {
812-
logger.log( "Commit and push ... ", ansi.error( " ERROR " ) + ", " + res );
810+
logger.log( "Commit and push ... " + result( 200 ) );
811+
}
812+
catch ( e ) {
813+
res = result.catch( e );
814+
815+
logger.log( "Commit and push ... " + ansi.error( " ERROR " ) + ", " + res );
816+
}
813817
}
814818
}
815819
}
820+
catch ( e ) {
821+
res = result.catch( e );
816822

817-
res.data.log = logger.flush().trim();
823+
logger.log( "Update dependencies ... " + ansi.error( " ETTOT " ) + ", " + res );
824+
}
825+
826+
if ( res.ok ) {
827+
res = result( 200 );
828+
}
829+
else {
830+
res = result( [ 500, "Dependencies update failed" ] );
831+
}
832+
833+
res.data = {
834+
"updates": updates.length
835+
? updates
836+
: null,
837+
"log": logger.flush().trim(),
838+
};
818839

819840
return res;
820841
}

0 commit comments

Comments
 (0)