diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index f038a6d..0aa7571 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -1,9 +1,8 @@ name: Deploy on: - push: - branches: - - master + schedule: + - cron: "0 * * * *" jobs: deploy: @@ -27,6 +26,7 @@ jobs: uses: actions/setup-node@v3 with: node-version: '18.x' + registry-url: 'https://registry.npmjs.org' - name: Cache node modules uses: actions/cache@v3 @@ -46,14 +46,23 @@ jobs: yarn global add @vscode/vsce ovsx yarn install - - name: Run deploy script - run: | - npm run build - npx tsx ./scripts/deploy.ts - npm publish --ignore-scripts - vsce publish -p ${{ env.VSCE_TOKEN }} - ovsx publish -p ${{ env.OVSX_TOKEN }} + - name: Build + run: npm run build + + - name: Run Deploy + run: npx tsx ./scripts/deploy.ts + + - name: Publish to NPM + run: npm publish --ignore-scripts || true env: NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} + + - name: Publish to VSCE + run: vsce publish -p ${{ env.VSCE_TOKEN }} || true + env: VSCE_TOKEN: ${{ secrets.VSCE_TOKEN }} + + - name: Publish to OVSX + run: ovsx publish -p ${{ env.OVSX_TOKEN }} || true + env: OVSX_TOKEN: ${{ secrets.OVSX_TOKEN }} diff --git a/package.json b/package.json index 0e3d888..0148a98 100644 --- a/package.json +++ b/package.json @@ -653,7 +653,7 @@ "detect-indent": "^6.0.0", "find-up": "^5.0.0", "mem": "^8.1.1", - "phpfmt": "^0.0.5", + "phpfmt": "^0.0.6", "pjson": "^1.0.9", "semver": "^7.5.0" } diff --git a/scripts/deploy.ts b/scripts/deploy.ts index 5de9c56..f8ea7a6 100644 --- a/scripts/deploy.ts +++ b/scripts/deploy.ts @@ -10,26 +10,13 @@ import { downloadFile } from '../src/utils'; const pkgJsonPath = path.join(__dirname, '../package.json'); const changelogPath = path.join(__dirname, '../CHANGELOG.md'); -const currentPharPath = path.join( - __dirname, - '../node_modules/phpfmt/v2/fmt.stub.php' -); void (async () => { try { const pkg = JSON.parse(String(await fs.promises.readFile(pkgJsonPath))); - const pharFilePath = phpfmt.v2.pharPath; const currentVersion = pkg.version; - const baseDir = path.resolve(pharFilePath, '..'); - const installFile = path.join(baseDir, 'install.js'); - const installContent = String(await fs.promises.readFile(installFile)); - const regex = /(var|const|let)\s+url\s+=\s+'(https?:\/\/[^']+)'/s; - const match = installContent.match(regex); - if (!match?.[2]) { - throw new Error('cannot find download url'); - } - const pharUrl = match[2]; + const pharUrl = phpfmt.v2.installUrl; console.log(`Download url: ${pharUrl}`); const tmpDir = path.join(os.tmpdir(), 'vscode-phpfmt'); @@ -38,7 +25,7 @@ void (async () => { await fs.promises.mkdir(tmpDir); } const currentVsixPath = path.join(tmpDir, `${currentVersion}.vsix`); - const latestPharPath = path.join(tmpDir, 'fmt.stub.php'); + const latestPharPath = path.join(tmpDir, phpfmt.v2.pharName); console.log('Downloading vsix...'); await downloadFile( @@ -46,10 +33,17 @@ void (async () => { currentVsixPath ); + const stats = await fs.promises.stat(currentVsixPath); + if (stats.size < 10000) { + console.log('Download vsix failed'); + return; + } + const zip = new AdmZip(currentVsixPath); const zipEntries = zip.getEntries(); const entry = zipEntries.find( - o => o.entryName === 'extension/node_modules/phpfmt/v2/fmt.stub.php' + o => + o.entryName === `extension/node_modules/phpfmt/v2/${phpfmt.v2.pharName}` ); const currentPharData = String(entry?.getData()); const currentMd5 = md5(currentPharData); @@ -72,7 +66,7 @@ void (async () => { let changelogData = String(await fs.promises.readFile(changelogPath)); changelogData = `### ${newVersion} -- Upgrade fmt.stub.php (${latestMd5}) +- Upgrade ${phpfmt.v2.pharName} (${latestMd5}) ${changelogData}`; await fs.promises.writeFile(changelogPath, changelogData); @@ -83,7 +77,7 @@ ${changelogData}`; JSON.stringify(pkg, null, 2) + os.EOL ); - await fs.promises.writeFile(currentPharPath, latestPharData); + await fs.promises.writeFile(phpfmt.v2.pharPath, latestPharData); const git = simpleGit({ config: [ @@ -101,7 +95,6 @@ ${changelogData}`; .addTag(`v${newVersion}`) .push() .pushTags(); - } catch (err) { console.error(err); process.exit(1); diff --git a/src/PHPFmtProvider.ts b/src/PHPFmtProvider.ts index 86d8751..d287f64 100644 --- a/src/PHPFmtProvider.ts +++ b/src/PHPFmtProvider.ts @@ -12,7 +12,6 @@ import { type QuickPickItem, type WorkspaceConfiguration } from 'vscode'; -import path from 'path'; import fs from 'fs'; import pkg from 'pjson'; import type { PHPFmt } from './PHPFmt'; @@ -62,29 +61,20 @@ export class PHPFmtProvider { const destFile = this.phpfmt.getFmt().pharPath; const bakFile = `${this.phpfmt.getFmt().pharPath}.bak`; - const baseDir = path.resolve(destFile, '..'); - const installFile = path.join(baseDir, 'install.js'); - const installContent = String(await fs.promises.readFile(installFile)); - const regex = /(var|const|let)\s+url\s+=\s+'(https?:\/\/[^']+)'/s; - const match = installContent.match(regex); - if (match?.[2]) { - const url = match[2]; - this.widget.logInfo(`Download url: ${url}`); + const url = this.phpfmt.getFmt().installUrl; + this.widget.logInfo(`Download url: ${url}`); - await fs.promises.copyFile(destFile, bakFile); + await fs.promises.copyFile(destFile, bakFile); - try { - await downloadFile(url, destFile); - this.widget.logInfo(`Download fmt to "${destFile}" successfully`); - await Window.showInformationMessage( - 'fmt.phar or fmt.stub.php upgraded successfully!' - ); - } catch (err) { - this.widget.logError('Download failed', err); - await fs.promises.copyFile(bakFile, destFile); - } - } else { - throw new Error('Failed to get url in modules'); + try { + await downloadFile(url, destFile); + this.widget.logInfo(`Download fmt to "${destFile}" successfully`); + await Window.showInformationMessage( + 'fmt.phar or fmt.stub.php upgraded successfully!' + ); + } catch (err) { + this.widget.logError('Download failed', err); + await fs.promises.copyFile(bakFile, destFile); } } catch (err) { await Window.showErrorMessage( diff --git a/yarn.lock b/yarn.lock index 12ee0f4..042dbb5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2386,10 +2386,10 @@ php-parser@^3.1.4: resolved "https://registry.yarnpkg.com/php-parser/-/php-parser-3.1.4.tgz#165a594bf9d7fb516b89d1ecf62a361083a482f8" integrity sha512-WUEfH4FWsVItqgOknM67msDdcUAfgPJsHhPNl6EPXzWtX+PfdY282m4i8YIJ9ALUEhf+qGDajdmW+VYqSd7Deg== -phpfmt@^0.0.5: - version "0.0.5" - resolved "https://registry.yarnpkg.com/phpfmt/-/phpfmt-0.0.5.tgz#bbbf5b8534308735453fd2a46606677fcd955e65" - integrity sha512-2ube9QII6r0nPTNIbFFAx0Zxt/kZKQws+xGdG2j4hZNbJtQ3zjOMFh9mcSMUYwTbNkHMJ+C+/8+cA2LFGpJ6nw== +phpfmt@^0.0.6: + version "0.0.6" + resolved "https://registry.yarnpkg.com/phpfmt/-/phpfmt-0.0.6.tgz#03ed77860e6ccf5037d851eec98e6aa012608ab3" + integrity sha512-Pw+4/73cNsi2ai1Oq3Qvvw7AagPYEetWCRvbiVgfbIJJFUajJocsblUpsUC6u0arFIrdZ951aHP0AyyVXl/avQ== dependencies: php-parser "^3.1.4"