11import path from "node:path" ;
22import fs from "node:fs/promises" ;
3- import { execSync } from "node:child_process" ;
3+ import { execFileSync } from "node:child_process" ;
44import * as tar from "tar" ;
55import native from "./native" ;
66import type { Platform } from "./types" ;
@@ -22,6 +22,33 @@ export interface PackOptions {
2222 description ?: string ;
2323}
2424
25+ function validateVersion ( version : string ) {
26+ if ( ! / ^ [ 0 - 9 ] + \. [ 0 - 9 ] + \. [ 0 - 9 ] + ( - [ 0 - 9 A - Z a - z . ] + ) ? $ / . test ( version ) ) {
27+ throw new Error ( `Invalid version: ${ version } ` ) ;
28+ }
29+ }
30+
31+ function validateNpmTag ( npmTag : string ) {
32+ if ( ! / ^ [ 0 - 9 A - Z a - z ] [ 0 - 9 A - Z a - z . _ - ] { 0 , 127 } $ / . test ( npmTag ) ) {
33+ throw new Error ( `Invalid npm tag: ${ npmTag } ` ) ;
34+ }
35+ }
36+
37+ function validatePathSegment ( name : string , value : string ) {
38+ if ( ! / ^ [ 0 - 9 A - Z a - z . _ - ] + $ / . test ( value ) || value . includes ( ".." ) ) {
39+ throw new Error ( `Invalid ${ name } : ${ value } ` ) ;
40+ }
41+ }
42+
43+ function validatePackagePrefix ( packagePrefix : string ) {
44+ const validPackagePrefix =
45+ / ^ ( @ [ 0 - 9 A - Z a - z . _ - ] + ( \/ [ 0 - 9 A - Z a - z . _ - ] + ) ? | [ 0 - 9 A - Z a - z . _ - ] + ) $ / ;
46+
47+ if ( ! validPackagePrefix . test ( packagePrefix ) ) {
48+ throw new Error ( `Invalid package prefix: ${ packagePrefix } ` ) ;
49+ }
50+ }
51+
2552async function packPlatform ( {
2653 platform,
2754 version,
@@ -31,11 +58,17 @@ async function packPlatform({
3158 srcDirPrefix = "dist" ,
3259 description
3360} : PackOptions ) : Promise < string > {
61+ validateVersion ( version ) ;
62+ validatePackagePrefix ( packagePrefix ) ;
63+ validatePathSegment ( "binary name" , binaryBaseName ) ;
64+ validatePathSegment ( "source directory prefix" , srcDirPrefix ) ;
65+
3466 const { os, arch } = platform ;
3567 console . log ( `Packing platform: ${ os } -${ arch } ` ) ;
3668 const npmDirName = `${ packagePrefix } -${ os } -${ arch } `
3769 . replace ( "@" , "" )
3870 . replace ( "/" , "-" ) ;
71+ validatePathSegment ( "package directory name" , npmDirName ) ;
3972 const tarballDir = path . join ( srcDir , "dist" , `${ npmDirName } -${ version } ` ) ;
4073 const scaffoldDir = path . join ( tarballDir , npmDirName ) ;
4174
@@ -81,12 +114,23 @@ async function packPlatform({
81114}
82115
83116function publishArtifacts ( artifacts : Array < string > , npmTag : string ) {
117+ validateNpmTag ( npmTag ) ;
118+
84119 for ( const artifact of artifacts ) {
85- const npmVersion = execSync ( "npm --version" ) . toString ( ) . trim ( ) ;
120+ const npmVersion = execFileSync ( "npm" , [ "--version" ] , {
121+ encoding : "utf8"
122+ } ) . trim ( ) ;
86123 console . log ( `npm version: ${ npmVersion } ` ) ;
87- const publishCommand = `npm publish "${ artifact } " --tag ${ npmTag } --access public` ;
88- console . log ( `Executing: ${ publishCommand } ` ) ;
89- execSync ( publishCommand , { stdio : "inherit" } ) ;
124+ console . log (
125+ `Executing: npm publish ${ artifact } --tag ${ npmTag } --access public`
126+ ) ;
127+ execFileSync (
128+ "npm" ,
129+ [ "publish" , artifact , "--tag" , npmTag , "--access" , "public" ] ,
130+ {
131+ stdio : "inherit"
132+ }
133+ ) ;
90134 }
91135}
92136
0 commit comments