Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
2a5230b
[storage-file-share] updated samples
witemple-msft Dec 9, 2019
dd98eb2
[prep-samples] allow files that don't have a matching import and just…
witemple-msft Dec 10, 2019
2fe35d9
[storage-file-share] typescript samples in shape
witemple-msft Dec 10, 2019
0d68724
[storage-file-share] made clean samples step clean up package locks i…
witemple-msft Dec 10, 2019
72e49cb
[prep-samples] added support for javascript samples
witemple-msft Dec 10, 2019
adb764c
[storage-file-share] Added dotenv and updated instructions
witemple-msft Dec 10, 2019
160ab84
[storage-file-share] Brought js samples in line with ts samples
witemple-msft Dec 10, 2019
8d1070d
[storage-file-share] bugfixes to ts samples packages
witemple-msft Dec 10, 2019
7951b5a
[storage-file-share] NPM scripts to run samples
witemple-msft Dec 10, 2019
03c7b22
[storage-file-share] remove execute-samples.js and use new runners
witemple-msft Dec 10, 2019
6adcb5d
[storage-file-share] added missing sample.env to js samples
witemple-msft Dec 11, 2019
9a793e6
[storage-file-share] Fixed sample package names
witemple-msft Dec 11, 2019
ef9a3d4
[storage-blob] update samples to be like storage-file-share
witemple-msft Dec 11, 2019
6ac61c7
[samples] Moved run-samples to common/scripts
witemple-msft Dec 11, 2019
12b3d00
[storage-file-share] Changed proxyAuth samples to exit if proxy infor…
witemple-msft Dec 11, 2019
5465f4c
[storage-file-share] changed iterators-handles to exit if share/direc…
witemple-msft Dec 11, 2019
8039b80
[run-samples] Error handling
witemple-msft Dec 11, 2019
e552211
[storage-file-share] Fixed sample errors due to prep-samples tree con…
witemple-msft Dec 11, 2019
b77c6b4
[storage-file-share] Changed from exceptions to a simple return in ca…
witemple-msft Dec 11, 2019
14e6fbe
[run-samples] Fixed an ignore bug
witemple-msft Dec 11, 2019
d2e70ca
[storage-file-share] Removed scripts and references to execute:all
witemple-msft Dec 11, 2019
1ab154b
[prep-samples] typo
witemple-msft Dec 11, 2019
b27c65c
[storage-file-share] Update sample.env
witemple-msft Dec 11, 2019
0a1edcf
[storage-blob] Replicate updates for storage-blob
witemple-msft Dec 11, 2019
8f8b6e2
[storage-blob] remove execute-samples.js
witemple-msft Dec 11, 2019
2de6076
[storage-blob] Fixed a bug in blob samples ts readme
witemple-msft Dec 12, 2019
5de8348
[storage-queue] Replicate updates to samples on storage-queue
witemple-msft Dec 12, 2019
30b38b4
[run-samples] made the runner give a brief digest of errors
witemple-msft Dec 12, 2019
46ac984
[storage] Made aad auth samples skippable when AAD information is not…
witemple-msft Dec 12, 2019
77c602d
[storage] changed proxyAuth samples to use HTTP[s]_PROXY vars
witemple-msft Dec 12, 2019
c1fd600
[storage-file-share] Fixed a bug in iterators sample when no queues e…
witemple-msft Dec 12, 2019
6f97268
[scripts] Make the scripts less wordy and fixed a bug in run-samples
witemple-msft Dec 12, 2019
98fa55c
[storage] Updated sample.env files to use HTTP_PROXY
witemple-msft Dec 12, 2019
787e631
[storage] Fixed wrong keywords in three sample packages.
witemple-msft Dec 16, 2019
1e25ece
[prep-samples] tsDir -> dir
witemple-msft Dec 16, 2019
d6653e5
[storage-queue] Removed need for sampleHelpers
witemple-msft Dec 16, 2019
e10e99b
[storage-file-share] Remove sampleHelpers
witemple-msft Dec 16, 2019
3145d3c
[storage-blob] removed sampleHelpers
witemple-msft Dec 16, 2019
3be4483
[run-samples] Removed outdated sampleHelpers.js from ignore list
witemple-msft Dec 16, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 43 additions & 19 deletions common/scripts/prep-samples.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ const fs =
* Breadth-first search for files ending in .ts, starting from `tsDir`
*
* @param {string} tsDir The root of the sample tree to search

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: tsDir => dir

* @param {(fs.Entry) => boolean} Predicate that decides whether or not a file entry is included
* @returns
*/
async function* findAllTsFiles(tsDir) {
async function* findMatchingFiles(tsDir, matches) {
const initialFiles = await fs.readdir(tsDir, { withFileTypes: true });

// BFS Queue and queue index
Expand All @@ -61,11 +62,7 @@ async function* findAllTsFiles(tsDir) {
for (const child of children) {
q.push([child, fullPath]);
}
} else if (
entry.isFile() &&
entry.name.endsWith(".ts") &&
!entry.name.endsWith(".d.ts")
) {
} else if (matches(entry)) {
yield fullPath;
} else if (
entry.isBlockDevice() ||
Expand All @@ -87,21 +84,26 @@ async function* findAllTsFiles(tsDir) {
}

/**
* Replaces package imports with relative imports for CI
* Replaces package require/import statements with relative pathsfor CI
Comment thread
willmtemple marked this conversation as resolved.
Outdated
*
* @param {string} file the name of the file to open and process
* @param {string} baseDir The base directory of the package
* @param {string} fileName the name of the file to open and process
* @param {string} baseDir the base directory of the package
* @param {string} pkgName name of the package to use when looking for package-local imports
*/
async function enableLocalRun(fileName, baseDir, pkgName) {
const fileContents = await fs.readFile(fileName, { encoding: "utf-8" });
const importRegex = new RegExp(
`import\\s+(.*)\\s+from\\s+"${pkgName}";?\\s?`,
"s"
);
const isTs = fileName.endsWith(".ts");
const importRegex = isTs
? new RegExp(`import\\s+(.*)\\s+from\\s+"${pkgName}";?\\s?`, "s")
Comment thread
richardpark-msft marked this conversation as resolved.
: new RegExp(`const\\s+(.*)\\s*=\\s*require\\("${pkgName}"\\);?\\s?`, "s");

if (!importRegex.exec(fileContents)) {
throw new Error(`Sample ${fileName} did not contain an import statement!`);
// With the newer methods of using helper files and batch running, this
// should be a warning
console.warn(
`[prep-samples] skipping ${fileName} because it did not contain a matching import/require`
);
return;
}

const relativeDir = path.dirname(fileName.replace(baseDir, ""));
Expand All @@ -112,10 +114,18 @@ async function enableLocalRun(fileName, baseDir, pkgName) {
const depth =
relativeDir.length - relativeDir.split(path.sep).join("").length;

const relativeImportPath = new Array(depth).fill("..").join("/") + "/src";
let relativePath = new Array(depth).fill("..").join("/");
Comment thread
jeremymeng marked this conversation as resolved.

if (isTs) {
// TypeScript imports should use src directly
relativePath += "/src";
}

const updatedContents = fileContents.replace(
importRegex,
`import $1 from "${relativeImportPath}";`
isTs
? `import $1 from "${relativePath}";`
: `const $1 = require("${relativePath}");`
);

return fs.writeFile(fileName, updatedContents, { encoding: "utf-8" });
Expand All @@ -132,15 +142,29 @@ async function main() {
baseDir = process.cwd();
}

const tsDir = path.join(baseDir, "samples", "typescript");
const package = require(path.join(baseDir, "package.json"));

console.log(
"[prep-samples] Preparing samples for package:",
`${package.name}@${package.version}`
);

for await (const fileName of findAllTsFiles(tsDir)) {
const tsDir = path.join(baseDir, "samples", "typescript", "src");
for await (const fileName of findMatchingFiles(
tsDir,
entry =>
entry.isFile() &&
entry.name.endsWith(".ts") &&
!entry.name.endsWith(".d.ts")
)) {
console.log("[prep-samples] Updating imports in", fileName);
await enableLocalRun(fileName, baseDir, package.name);
}

const jsDir = path.join(baseDir, "samples", "javascript");
for await (const fileName of findMatchingFiles(
jsDir,
entry => entry.isFile() && entry.name.endsWith(".js")
)) {
console.log("[prep-samples] Updating imports in", fileName);
await enableLocalRun(fileName, baseDir, package.name);
}
Expand Down
80 changes: 0 additions & 80 deletions sdk/storage/storage-file-share/execute-samples.js

This file was deleted.

10 changes: 6 additions & 4 deletions sdk/storage/storage-file-share/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@
"build:autorest": "autorest ./swagger/README.md --typescript --package-version=12.0.0-preview.7 --use=@microsoft.azure/autorest.typescript@5.0.1",
"build:es6": "tsc -p tsconfig.json",
"build:nodebrowser": "rollup -c 2>&1",
"build:js-samples": "npm run clean && npm run build:es6 && cross-env ONLY_NODE=true rollup -c 2>&1",
"build:ts-samples": "npm run clean && node ../../../common/scripts/prep-samples.js && cd samples && tsc -p . ",
"build:samples": "npm run clean && npm run build:es6 && cross-env ONLY_NODE=true rollup -c 2>&1 && npm run build:prep-samples",
"build:prep-samples": "node ../../../common/scripts/prep-samples.js && cd samples && tsc",
"build:test": "npm run build:es6 && rollup -c rollup.test.config.js 2>&1",
"build": "npm run build:es6 && npm run build:nodebrowser && api-extractor run --local",
"check-format": "prettier --list-different --config ../../.prettierrc.json \"src/**/*.ts\" \"test/**/*.ts\" \"*.{js,json}\"",
"clean": "rimraf dist dist-esm dist-test typings temp browser/*.js* browser/*.zip statistics.html coverage coverage-browser .nyc_output *.tgz *.log test*.xml TEST*.xml",
"clean:samples": "rimraf samples/javascript/node_modules samples/typescript/node_modules samples/typescript/dist",
"clean:samples": "rimraf samples/javascript/node_modules samples/typescript/node_modules samples/typescript/dist samples/typescript/package-lock.json samples/javascript/package-lock.json",
"extract-api": "tsc -p . && api-extractor run --local",
"execute:samples": "echo skipped for now due to https://github.com/Azure/azure-sdk-for-js/issues/6362",
"execute:js-samples": "cross-env BATCH_RUN_SAMPLES=true node samples/javascript/run.js",
"execute:ts-samples": "cross-env BATCH_RUN_SAMPLES=true node samples/typescript/dist/samples/typescript/src/run.js",
"execute:samples": "npm run build:samples && npm run execute:js-samples && npm run execute:ts-samples",
"format": "prettier --write --config ../../.prettierrc.json \"src/**/*.ts\" \"test/**/*.ts\" \"*.{js,json}\"",
"integration-test:browser": "karma start --single-run",
"integration-test:node": "nyc mocha --require source-map-support/register --reporter mocha-multi --reporter-options spec=-,mocha-junit-reporter=- --full-trace -t 120000 --retries 2 dist-test/index.node.js",
Expand Down
18 changes: 16 additions & 2 deletions sdk/storage/storage-file-share/samples/javascript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ These sample programs show how to use the JavaScript client libraries for Azure

The sample are compatible with Node.js >= 8.0.0, except for the samples that use the async `for await` syntax, which require Node.js >= 10.0.0.

You need [an Azure subscription][freesub] and [an Azure Storage account][azstorage] to run these sample programs. Samples retrieve credentials to access the storage account from environment variables. See each individual sample for details on which environment variables it requires to function.
You need [an Azure subscription][freesub] and [an Azure Storage account][azstorage] to run these sample programs. Samples retrieve credentials to access the storage account from environment variables. Alternatively, edit the source code to include the appropriate credentials. See each individual sample for details on which environment variables/credentials it requires to function.

Adapting the samples to run in the browser requires some additional consideration. For details, please see the [package README][package].

Expand All @@ -43,12 +43,26 @@ To run the samples using the published version of the package:
npm install
```

2. Run the sample with the correct environment variables set, for example (cross-platform):
2. Edit the file `sample.env`, adding the correct credentials to access the Azure service and run the samples. Then rename the file from `sample.env` to just `.env`. The sample programs will read this file automatically.

3. Run whichever samples you like (note that some samples may require additional setup, see the table above):

```bash
node basic.js
```

Alternatively, run a single sample with the correct environment variables set (step 3 is not required if you do this), for example (cross-platform):

```bash
npx cross-env ACCOUNT_NAME="<account name>" ACCOUNT_KEY="<account key>" node basic.js
```

Or, to run all of the samples, use the following npm command:

```bash
npm run execute:all
```

## Next Steps

Take a look at our [API Documentation][apiref] for more information about the APIs that are available in the clients.
Expand Down
20 changes: 11 additions & 9 deletions sdk/storage/storage-file-share/samples/javascript/advanced.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

/*
Setup: Enter your storage account name, SAS and a path pointing to local file in main()
*/
Expand All @@ -10,6 +13,8 @@ const {
newPipeline
} = require("@azure/storage-file-share");

const { runSample } = require("./sampleHelpers");

// Enabling logging may help uncover useful information about failures.
// In order to see a log of HTTP requests and responses, set the `AZURE_LOG_LEVEL` environment variable to `info`.
// Alternatively, logging can be enabled at runtime by calling `setLogLevel("info");`
Expand All @@ -21,7 +26,7 @@ async function main() {
// Fill in following settings before running this sample
const account = process.env.ACCOUNT_NAME || "";
const accountSas = process.env.ACCOUNT_SAS || "";
const localFilePath = "../README.md";
const localFilePath = "README.md";

const pipeline = newPipeline(new AnonymousCredential(), {
// httpClient: MyHTTPClient, // A customized HTTP client implementing IHttpClient interface
Expand Down Expand Up @@ -99,11 +104,8 @@ async function main() {
console.log("deleted share");
}

// An async method returns a Promise object, which is compatible with then().catch() coding style.
main()
.then(() => {
console.log("Successfully executed sample.");
})
.catch((err) => {
console.log(err.message);
});
runSample(main).catch((err) => {
console.error("Error running sample:", err.message);
});

module.exports = { main };
18 changes: 10 additions & 8 deletions sdk/storage/storage-file-share/samples/javascript/anonymousCred.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

/*
Setup: Enter your storage account name and SAS in main()
*/

const { ShareServiceClient, AnonymousCredential } = require("@azure/storage-file-share");

const { runSample } = require("./sampleHelpers");

async function main() {
// Enter your storage account name and SAS
const account = process.env.ACCOUNT_NAME || "";
Expand Down Expand Up @@ -36,11 +41,8 @@ async function main() {
console.log(`deleted share ${shareName}`);
}

// An async method returns a Promise object, which is compatible with then().catch() coding style.
main()
.then(() => {
console.log("Successfully executed sample.");
})
.catch((err) => {
console.log(err.message);
});
runSample(main).catch((err) => {
console.error("Error running sample:", err.message);
});

module.exports = { main };
18 changes: 10 additions & 8 deletions sdk/storage/storage-file-share/samples/javascript/basic.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

/*
Setup: Enter your storage account name and shared key in main()
*/

const { ShareServiceClient, StorageSharedKeyCredential } = require("@azure/storage-file-share");

const { runSample } = require("./sampleHelpers");

async function main() {
// Enter your storage account name and shared key
const account = process.env.ACCOUNT_NAME || "";
Expand Down Expand Up @@ -90,11 +95,8 @@ async function streamToString(readableStream) {
});
}

// An async method returns a Promise object, which is compatible with then().catch() coding style.
main()
.then(() => {
console.log("Successfully executed sample.");
})
.catch((err) => {
console.log(err.message);
});
runSample(main).catch((err) => {
console.error("Error running sample:", err.message);
});

module.exports = { main };
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

/*
Setup: Enter your storage account name and shared key in main()
*/
Expand All @@ -8,6 +11,8 @@ const {
newPipeline
} = require("@azure/storage-file-share");

const { runSample } = require("./sampleHelpers");

async function main() {
// Enter your storage account name and shared key
const account = process.env.ACCOUNT_NAME || "";
Expand Down Expand Up @@ -47,11 +52,8 @@ async function main() {
console.log(`deleted share ${shareName}`);
}

// An async method returns a Promise object, which is compatible with then().catch() coding style.
main()
.then(() => {
console.log("Successfully executed sample.");
})
.catch((err) => {
console.log(err.message);
});
runSample(main).catch((err) => {
console.error("Error running sample:", err.message);
});

module.exports = { main };
Loading